How to implement Java recursive processing of permission management menu tree or classification

This article mainly introduces how to implement Java recursive processing of permission management menu tree or classification. It is introduced in great detail through the example code, which has a certain reference value for everyone's study or work. Friends in need can refer to it

1. Database table design

2. Entity design

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. Recursive tool class

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;
	}
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>