This article mainly introduces how to realize the Java recursive processing authority management menu tree or classification. The article introduces in detail through the example code, which has certain reference learning value for everyone’s study or work, and friends in need can refer to it
1. Database table design
2. Entity class design
package com.ieou.capsule.dto.SystemPermissions;
import java.util.List;
/**
*Function menu class
*/
public class SystemPermissionsTree {
private String functionCode;
//Menu code
private String parentFunctionCode;
//Parent menu code
private String functionName;
//Menu name
private Boolean flag;
//True: checked false: not selected
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 {
/**
*Author: Yi Mufeng Yi
*Source: CSDN
*Original text: https://blog.csdn.net/gxgl8811/article/details/72803833
*Copyright notice: This article is the original article of blogger, please attach the link of blog!
*/
public static List<SystemPermissionsTree> getTreeList(List<SystemPermissionsTree> entityList) {
List<SystemPermissionsTree> resultList = new ArrayList<>();
//Get top level element collection
String parentCode;
for (SystemPermissionsTree entity : entityList) {
parentCode = entity.getParentFunctionCode();
//The parentcode of the top-level element is null or 0
if (parentCode == null || "0".equals(parentCode)) {
resultList.add(entity);
}
}
//Gets the child data collection of each top-level element
for (SystemPermissionsTree entity : resultList) {
entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
}
return resultList;
}
/**
*Get sub data collection
*
* @param id
* @param entityList
* @return
* @author jianda
*@ date 29 may 2017
*/
private static List<SystemPermissionsTree> getSubList(String id, List<SystemPermissionsTree> entityList) {
List<SystemPermissionsTree> childList = new ArrayList<>();
String parentId;
//Direct subobject of subset
for (SystemPermissionsTree entity : entityList) {
parentId = entity.getParentFunctionCode();
if (id.equals(parentId)) {
childList.add(entity);
}
}
//Indirect subobjects of subsets
for (SystemPermissionsTree entity : childList) {
entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
}
//Recursive exit condition
if (childList.size() == 0) {
return null;
}
return childList;
}
}
The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.