springmvc+mybatis 无极限树形结构菜单

MYSQL数据库

springmvc+mybatis 无极限树形结构菜单_第1张图片


查出数据调用封装类

	List menulist = new ArrayList();
		menulist = menuService.menuList();
		MenuTree menuTree = new MenuTree();
		System.out.println(menuTree.menuList(menulist));

树形结构封装类

package com.sdmc.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.sdmc.bean.Menu;

public class MenuTree {
	
	
	public static Map mapArray = new LinkedHashMap();
	public List menuCommon;
	public List list = new ArrayList();
	
	public List menuList(List menu){    
		this.menuCommon = menu;
		for (Menu x : menu) {   
			Map mapArr = new LinkedHashMap();
			if(x.getPid()==0){
				mapArr.put("id", x.getId());
				mapArr.put("name", x.getName());
				mapArr.put("level", x.getLevel());
				mapArr.put("pid", x.getPid());	
				mapArr.put("child", menuChild(x.getId()));	
				list.add(mapArr);
			}
		}	
		return list;
	}
	
	
	public List menuChild(int id){
		List lists = new ArrayList();
		for(Menu a:menuCommon){
			Map childArray = new LinkedHashMap();
			if(a.getPid() == id){
				childArray.put("id", a.getId());
				childArray.put("name", a.getName());
				childArray.put("level", a.getLevel());
				childArray.put("pid", a.getPid());
				childArray.put("child", menuChild(a.getId()));	
				lists.add(childArray);
			}
		}
		return lists;
		
	}
} 
  

结果

[{id=1, name=内容管理, level=1, pid=0, 
	child=[{id=6, name=栏目管理, level=2, pid=1, 
		child=[{id=17, name=管理内容, level=3, pid=6, 
			child=[{id=11, name=业务管理, level=4, pid=17, child=[]}]}]},
		{id=7, name=模型管理, level=2, pid=1, child=[]}]},
{id=2, name=用户管理, level=1, pid=0, child=[{id=10, name=会员管理, level=2, pid=2, child=[]}]}
]




你可能感兴趣的:(Spring)