java处理多级菜单和多层树的思考-递归篇

  • Post author:
  • Post category:java


  • 2021-06-25



处理思路和方法

多级菜单中 父节点和子节点的关系 如表所示

  • id :代表每个菜单的唯一id不重复
  • menuName:代表每个菜单的名字
  • parentNode :代表该节点若为其他节点的子节点时其parentNode的值为父节点的id 其中parentNode为-1的时候代表其为根节点
  • childrenNode: 代表其是否有子节点 有子节点时值为1 反则值为0
id menuName parentNode childrenNode
0 体育 -1 1
1 足球 0 0
2 篮球 0 0
3 排球 0 0
4 音乐 -1 0
5 书籍 -1 1
6 第一本书 5 0
7 第二本书 5 0
8 第三本书 5 0

表关系已经建好 ,因为父子节点的关系通过id 和parentNode进行了关联。如何把数据组装成Json的那种层级鲜明的样子呢。代码中用递归的方式 如下面代码所示。



代码

pojo Hobby的代码 。 代码中的hobbyname为上面表格中的MenuName

public class Hobby {
	private int id;
	private String hobbyname;
	private int parentNode;
	private int childrenNode; // 1代表有子节点 0代表没有子节点
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getHobbyname() {
		return hobbyname;
	}
	public void setHobbyname(String hobbyname) {
		this.hobbyname = hobbyname;
	}
	public int getParentNode() {
		return parentNode;
	}
	public void setParentNode(int parentNode) {
		this.parentNode = parentNode;
	}
	public int getChildrenNode() {
		return childrenNode;
	}
	public void setChildrenNode(int childrenNode) {
		this.childrenNode = childrenNode;
	}
	public Hobby(int id, String hobbyname, int parentNode, int childrenNode) {
		super();
		this.id = id;
		this.hobbyname = hobbyname;
		this.parentNode = parentNode;
		this.childrenNode = childrenNode;
	}
	public Hobby() {
		super();
	}
}

初始化数据的代码,组成一个list集合

    public static List<Hobby> init() {
    	 Hobby tiyu = new Hobby(0, "体育", -1, 1);
         Hobby football = new Hobby(1, "足球", 0, 0);
         Hobby basketball = new Hobby(2, "篮球", 0, 0);
         Hobby volleyball = new Hobby(3, "排球", 0, 0);
         Hobby music = new Hobby(4,"音乐",-1,0);
         Hobby book = new Hobby(5,"书籍",-1,1);
         Hobby book1 = new Hobby(6,"第一本书",5,0);
         Hobby book2 = new Hobby(7,"第二本书",5,0);
         Hobby book3 = new Hobby(8,"第三本书",5,0);
        List<Hobby> hobbies = new LinkedList<Hobby>();
        hobbies.add(tiyu);
        hobbies.add(football);
        hobbies.add(basketball);
        hobbies.add(volleyball);
        hobbies.add(music);
        hobbies.add(book);
        hobbies.add(book1);
        hobbies.add(book2);
        hobbies.add(book3);
        return hobbies;
    }

递归组装成 json那种层级分明的形式

 public static List<Map<String, Object>> digui(List<Hobby> hobbies, int i){
        List<Map<String, Object>> maps = new LinkedList<Map<String,Object>>();
        for(Hobby hobby: hobbies) {
            Map<String, Object> map = new HashMap<String, Object>();

            if(i == hobby.getParentNode()) { // 把自己的id下发 得到节点中父节点等于自己的节点
                map.put("id", hobby.getId());
                map.put("hobbyname", hobby.getHobbyname());
                map.put("parentNode", hobby.getParentNode());
                map.put("childrenNode", hobby.getChildrenNode());

                if(hobby.getChildrenNode()==1){ // 是否有子菜单查找
                    map.put("children",digui(hobbies, hobby.getId()));
                }
                maps.add(map);
            }

        }
        return maps;
    }

调用方式 传入-1 ,-1代表根节点

	
	public List<Map<String, Object>> selectAll(){
		List<Hobby> listAll = this.init();
		List<Map<String, Object>> hobbyies = this.digui(listAll, -1);
		return hobbyies;
	}



版权声明:本文为qq_38517543原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。