JAVA递归生成树形菜单的实现过程

递归生成一个如图的菜单,编写两个类数据模型Menu、和创建树形的MenuTree。通过以下过程实现:
1.首先从菜单数据中获取所有根节点。
【JAVA递归生成树形菜单的实现过程】2.为根节点建立次级子树并拼接上。
3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”。
JAVA递归生成树形菜单的实现过程
文章图片

首先,编写数据模型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 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 menuList = new ArrayList(); public MenuTree(List menuList) {this.menuList=menuList; }//建立树形结构public List builTree(){List treeMenus =newArrayList(); for(Menu menuNode : getRootNode()) {menuNode=buildChilTree(menuNode); treeMenus.add(menuNode); }return treeMenus; }//递归,建立子树形结构private Menu buildChilTree(Menu pNode){List chilMenus =newArrayList(); for(Menu menuNode : menuList) {if(menuNode.getParentId().equals(pNode.getId())) {chilMenus.add(buildChilTree(menuNode)); }}pNode.setChildren(chilMenus); return pNode; }//获取根节点private List getRootNode() {List rootMenuLists =newArrayList(); 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) {ListmenuList= new ArrayList(); /*插入一些数据*/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 children = new ArrayList(); 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 getChildren() {return children; } public void setChildren(List children) {this.children = children; }}

三、递归组装树形结构函数实现
/*** @方法名: parseMenuTree
* @描述: 组装菜单
* @param list 数据库里面获取到的全量菜单列表* @return*/public static List parseMenuTree(List list){List result = new ArrayList(); // 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 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 list = new ArrayList(); //TODO 这里从数据库获取全量菜单后放到list中//树形结构数据生成List result = parseMenuTree(list); System.out.println(JSONObject.toJSONString(result)); }

到此这篇关于JAVA递归生成树形菜单的文章就介绍到这了,更多相关JAVA递归生成树形菜单内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读