溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)java遞歸 處理權(quán)限管理菜單樹(shù)或分類

發(fā)布時(shí)間:2020-08-22 07:25:40 來(lái)源:腳本之家 閱讀:272 作者:低調(diào)的小白 欄目:編程語(yǔ)言

這篇文章主要介紹了如何實(shí)現(xiàn)java遞歸 處理權(quán)限管理菜單樹(shù)或分類,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1.數(shù)據(jù)庫(kù)表設(shè)計(jì)

如何實(shí)現(xiàn)java遞歸 處理權(quán)限管理菜單樹(shù)或分類

2.實(shí)體類設(shè)計(jì)

package com.ieou.capsule.dto.SystemPermissions;
import java.util.List;
/**
 * 功能菜單類
 */
public class SystemPermissionsTree {
	private String functionCode;
	//菜單碼
	private String parentFunctionCode;
	//父級(jí)菜單碼
	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 {
	/**
   * 作者:一沐楓一
   * 來(lái)源:CSDN
   * 原文:https://blog.csdn.net/gxgl8811/article/details/72803833
   * 版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
   */
	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);
			}
		}
		//獲取每個(gè)頂層元素的子數(shù)據(jù)集合
		for (SystemPermissionsTree entity : resultList) {
			entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
		}
		return resultList;
	}
	/**
   * 獲取子數(shù)據(jù)集合
   *
   * @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;
		//子集的直接子對(duì)象
		for (SystemPermissionsTree entity : entityList) {
			parentId = entity.getParentFunctionCode();
			if (id.equals(parentId)) {
				childList.add(entity);
			}
		}
		//子集的間接子對(duì)象
		for (SystemPermissionsTree entity : childList) {
			entity.setChildrenList(getSubList(entity.getFunctionCode(), entityList));
		}
		//遞歸退出條件
		if (childList.size() == 0) {
			return null;
		}
		return childList;
	}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI