栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

如何实现java递归 处理权限管理菜单树或分类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何实现java递归 处理权限管理菜单树或分类

这篇文章主要介绍了如何实现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 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 getChildrenList() {
		return childrenList;
	}
	public void setChildrenList(List 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 {
	
	public static List getTreeList(List entityList) {
		List 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;
	}
	
	private static List getSubList(String id, List entityList) {
		List 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;
	}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135557.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号