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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-04-02 我要评论

这篇文章主要介绍了如何实现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;
	}
}
您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们