如何實現(xiàn)java遞歸 處理權(quán)限管理菜單樹或分類
這篇文章主要介紹了如何實現(xiàn)java遞歸 處理權(quán)限管理菜單樹或分類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
1.數(shù)據(jù)庫表設(shè)計
2.實體類設(shè)計
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 * 版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接! */ 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); } } //獲取每個頂層元素的子數(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; //子集的直接子對象 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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用java.security.SecureRandom安全生成隨機(jī)數(shù)和隨機(jī)字符串工具類
這篇文章主要給大家介紹了關(guān)于如何使用java.security.SecureRandom安全生成隨機(jī)數(shù)和隨機(jī)字符串工具類的相關(guān)資料,SecureRandom擴(kuò)展了Random類,并通過在java 8中添加的新方法得到了豐富,需要的朋友可以參考下2024-05-05mybatis-plus配置控制臺打印完整帶參數(shù)SQL語句的實現(xiàn)
這篇文章主要介紹了mybatis-plus配置控制臺打印完整帶參數(shù)SQL語句,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08SpringBoot和前端聯(lián)動實現(xiàn)存儲瀏覽記錄功能
這篇文章主要介紹了SpringBoot和前端聯(lián)動實現(xiàn)存儲瀏覽記錄功能,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01Java實現(xiàn)將類數(shù)據(jù)逐行寫入CSV文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實現(xiàn)將類數(shù)據(jù)逐行寫入CSV文件,文中的示例代碼講解詳細(xì),具有一定的參考價值,需要的可以借鑒一下2022-11-11