1. 程式人生 > 程式設計 >如何實現java遞迴 處理許可權管理選單樹或分類

如何實現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<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;
	}
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。