这篇文章主要介绍了如何实现java递归 处理权限管理菜单树或分类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.数据库表设计
2.实体类设计
package com.ieou.capsule.dto.SystemPermissions;
import java.util.List;
/**
* 功能菜单类
*/
public class SystemPermissionsTree {
private String functionCode;
//菜单码
private String parentFunctionCode;
//父级菜单码
private String functionName;
//菜单名
private Boolean flag;
// true:选中 false:未选中
private List<SystemPermissionsTree> childrenList;
public String getFunctionCode() {
return functionCode;
}
public void setFunctionCode(String functionCode) {
this.functionCode = functionCode;
}
public String getParentFunctionCode() {
return parentFunctionCode;
}
public void setParentFunctionCode(String parentFunctionCode) {
this.parentFunctionCode = parentFunctionCode;
}
public String getFunctionName() {
return functionName;
}
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
public List<SystemPermissionsTree> getChildrenList() {
return childrenList;
}
public void setChildrenList(List<SystemPermissionsTree> childrenList) {
this.childrenList = childrenList;
}
}
3.递归工具类
package com.ieou.capsule.util;
import com.ieou.capsule.dto.SystemPermissions.SystemPermissionsTree;
import java.util.ArrayList;
import java.util.List;
public class TreeUtil {
/**
* 作者:一沐枫一
* 来源:CSDN
* 原文:https://blog.csdn.net/gxgl8811/article/details/72803833
* 版权声明:本文为博主原创文章,转载请附上博文链接!
*/
public static List<SystemPermissionsTree> getTreeList(List<SystemPermissionsTree> entityList) {
List<SystemPermissionsTree> resultList = new ArrayList<>();
//获取顶层元素集合
String parentCode;
for (SystemPermissionsTree entity : entityList) {
parentCode = entity.getParentFunctionCode();
//顶层元素的parentCode==null或者为0
if (parentCode == null || "0".equals(parentCode)) {
resultList.add(entity);
}
}
//获取每个顶层元素的子数据集合
for (SystemPermissionsTree entity : resultList) {
entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
}
return resultList;
}
/**
* 获取子数据集合
*
* @param id
* @param entityList
* @return
* @author jianda
* @date 2017年5月29日
*/
private static List<SystemPermissionsTree> getSubList(String id, List<SystemPermissionsTree> entityList) {
List<SystemPermissionsTree> childList = new ArrayList<>();
String parentId;
//子集的直接子对象
for (SystemPermissionsTree entity : entityList) {
parentId = entity.getParentFunctionCode();
if (id.equals(parentId)) {
childList.add(entity);
}
}
//子集的间接子对象
for (SystemPermissionsTree entity : childList) {
entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
}
//递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。