java递归 处理权限管理菜单树或分类

本文介绍了一个用于权限管理的功能菜单实体类SystemPermissionsTree的设计,以及如何使用递归算法通过TreeUtil工具类构建权限菜单树。文章详细展示了实体类的属性与getter/setter方法,同时提供了获取顶层菜单及子菜单的递归方法实现。

1.数据库表设计

2.实体类设计

 1 package com.ieou.capsule.dto.SystemPermissions;
 2 
 3 import java.util.List;
 4 
 5 /**
 6  * 功能菜单类
 7  */
 8 public class SystemPermissionsTree {
 9 
10     private String functionCode;//菜单码
11 
12     private String parentFunctionCode;//父级菜单码
13 
14     private String functionName;//菜单名
15 
16     private Boolean flag; // true:选中   false:未选中
17 
18     private List<SystemPermissionsTree> childrenList;
19 
20     public String getFunctionCode() {
21         return functionCode;
22     }
23 
24     public void setFunctionCode(String functionCode) {
25         this.functionCode = functionCode;
26     }
27 
28     public String getParentFunctionCode() {
29         return parentFunctionCode;
30     }
31 
32     public void setParentFunctionCode(String parentFunctionCode) {
33         this.parentFunctionCode = parentFunctionCode;
34     }
35 
36     public String getFunctionName() {
37         return functionName;
38     }
39 
40     public void setFunctionName(String functionName) {
41         this.functionName = functionName;
42     }
43 
44     public Boolean getFlag() {
45         return flag;
46     }
47 
48     public void setFlag(Boolean flag) {
49         this.flag = flag;
50     }
51 
52     public List<SystemPermissionsTree> getChildrenList() {
53         return childrenList;
54     }
55 
56     public void setChildrenList(List<SystemPermissionsTree> childrenList) {
57         this.childrenList = childrenList;
58     }
59 }

3.递归工具类

 1 package com.ieou.capsule.util;
 2 
 3 import com.ieou.capsule.dto.SystemPermissions.SystemPermissionsTree;
 4 
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 public class TreeUtil {
 9     /**
10      * 作者:一沐枫一
11      * 来源:优快云
12      * 原文:https://blog.youkuaiyun.com/gxgl8811/article/details/72803833
13      * 版权声明:本文为博主原创文章,转载请附上博文链接!
14      */
15 
16     public static List<SystemPermissionsTree> getTreeList(List<SystemPermissionsTree> entityList) {
17         List<SystemPermissionsTree> resultList = new ArrayList<>();
18 
19         //获取顶层元素集合
20         String parentCode;
21         for (SystemPermissionsTree entity : entityList) {
22             parentCode = entity.getParentFunctionCode();
23             //顶层元素的parentCode==null或者为0
24             if (parentCode == null || "0".equals(parentCode)) {
25                 resultList.add(entity);
26             }
27         }
28 
29         //获取每个顶层元素的子数据集合
30         for (SystemPermissionsTree entity : resultList) {
31             entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
32         }
33 
34         return resultList;
35     }
36 
37     /**
38      * 获取子数据集合
39      *
40      * @param id
41      * @param entityList
42      * @return
43      * @author jianda
44      * @date 2017年5月29日
45      */
46     private static List<SystemPermissionsTree> getSubList(String id, List<SystemPermissionsTree> entityList) {
47         List<SystemPermissionsTree> childList = new ArrayList<>();
48         String parentId;
49 
50         //子集的直接子对象
51         for (SystemPermissionsTree entity : entityList) {
52             parentId = entity.getParentFunctionCode();
53             if (id.equals(parentId)) {
54                 childList.add(entity);
55             }
56         }
57 
58         //子集的间接子对象
59         for (SystemPermissionsTree entity : childList) {
60             entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
61         }
62 
63         //递归退出条件
64         if (childList.size() == 0) {
65             return null;
66         }
67 
68         return childList;
69     }
70 
71 
72 }

 

转载于:https://www.cnblogs.com/wang-yaz/p/9816132.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值