java递归树形菜单
lucky_pin 人气:0递归生成一个如图的菜单,编写两个类数据模型Menu、和创建树形的MenuTree。通过以下过程实现:
1.首先从菜单数据中获取所有根节点。
2.为根节点建立次级子树并拼接上。
3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”。
首先,编写数据模型Menu。每条菜单有自己的id、父节点parentId、菜单名称text、菜单还拥有次级菜单children。
import java.util.List; public class Menu { private String id; private String parentId; private String text; private String url; private String yxbz; private List<Menu> children; public Menu(String id,String parentId,String text,String url,String yxbz) { this.id=id; this.parentId=parentId; this.text=text; this.url=url; this.yxbz=yxbz; } /*省略get\set*/ }
创建树形结构的类MenuTree。方法getRootNode获取所有根节点,方法builTree将根节点汇总创建树形结构,buildChilTree为节点建立次级树并拼接上当前树,递归调用buildChilTree不断为当前树开枝散叶直至找不到新的子树。完成递归,获取树形结构。
import java.util.ArrayList; import java.util.List; public class MenuTree { private List<Menu> menuList = new ArrayList<Menu>(); public MenuTree(List<Menu> menuList) { this.menuList=menuList; } //建立树形结构 public List<Menu> builTree(){ List<Menu> treeMenus =new ArrayList<Menu>(); for(Menu menuNode : getRootNode()) { menuNode=buildChilTree(menuNode); treeMenus.add(menuNode); } return treeMenus; } //递归,建立子树形结构 private Menu buildChilTree(Menu pNode){ List<Menu> chilMenus =new ArrayList<Menu>(); for(Menu menuNode : menuList) { if(menuNode.getParentId().equals(pNode.getId())) { chilMenus.add(buildChilTree(menuNode)); } } pNode.setChildren(chilMenus); return pNode; } //获取根节点 private List<Menu> getRootNode() { List<Menu> rootMenuLists =new ArrayList<Menu>(); for(Menu menuNode : menuList) { if(menuNode.getParentId().equals("0")) { rootMenuLists.add(menuNode); } } return rootMenuLists; } }
最后,插入一些数据试试效果。得到的json就可以生成图一菜单了。
import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.JSON; public class Hello { public static void main(String []args) { List<Menu> menuList= new ArrayList<Menu>(); /*插入一些数据*/ menuList.add(new Menu("GN001D000","0","系统管理","/admin","Y")); menuList.add(new Menu("GN001D100","GN001D000","权限管理","/admin","Y")); menuList.add(new Menu("GN001D110","GN001D100","密码修改","/admin","Y")); menuList.add(new Menu("GN001D120","GN001D100","新加用户","/admin","Y")); menuList.add(new Menu("GN001D200","GN001D000","系统监控","/admin","Y")); menuList.add(new Menu("GN001D210","GN001D200","在线用户","/admin","Y")); menuList.add(new Menu("GN002D000","0","订阅区","/admin","Y")); menuList.add(new Menu("GN003D000","0","未知领域","/admin","Y")); /*让我们创建树*/ MenuTree menuTree =new MenuTree(menuList); menuList=menuTree.builTree(); /*转为json看看效果*/ String jsonOutput= JSON.toJSONString(menuList); System.out.println(jsonOutput); } }
补充:java递归生成树形结构菜单
一、mysql表,数据自行准备
CREATE TABLE `sys_menu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pid` bigint(20) DEFAULT NULL, `title` varchar(255) CHARACTER SET utf8 DEFAULT NULL, `path` varchar(255) CHARACTER SET utf8 DEFAULT NULL, `level` int(11) DEFAULT NULL, `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=200 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
二、创建对应的实体类
public class Menu implements Serializable { private static final long serialVersionUID = -5990021029947688358L; private Integer id; private String title;//菜单标题 private String path;//路径 private Integer pid;//父菜单ID 一级菜单pid为null private Integer level;//级别,排序用 private List<Menu> children = new ArrayList<Menu>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; } public List<Menu> getChildren() { return children; } public void setChildren(List<Menu> children) { this.children = children; } }
三、递归组装树形结构函数实现
/** * @方法名: parseMenuTree<br> * @描述: 组装菜单<br> * @param list 数据库里面获取到的全量菜单列表 * @return */ public static List<Menu> parseMenuTree(List<Menu> list){ List<Menu> result = new ArrayList<Menu>(); // 1、获取第一级节点 for (Menu menu : list) { if(null == menu.getPid()) { result.add(menu); } } // 2、递归获取子节点 for (Menu parent : result) { parent = recursiveTree(parent, list); } return result; } public static Menu recursiveTree(Menu parent, List<Menu> list) { for (Menu menu : list) { if(Objects.equals(parent.getId(),menu.getPid())) { menu = recursiveTree(menu, list); parent.getChildren().add(menu); } } return parent; }
四、从数据库获取菜单数据,调用组装菜单函数生成树形结构的数据
public static void main(String[] args) { List<Menu> list = new ArrayList<Menu>(); //TODO 这里从数据库获取全量菜单后放到list中 //树形结构数据生成 List<Menu> result = parseMenuTree(list); System.out.println(JSONObject.toJSONString(result)); }
加载全部内容