如何实现java递归 处理权限管理菜单树或分类
这篇文章主要介绍了如何实现java递归处理权限管理菜单树或分类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.数据库表设计
2.实体类设计
packagecom.ieou.capsule.dto.SystemPermissions;
importjava.util.List;
/**
*功能菜单类
*/
publicclassSystemPermissionsTree{
privateStringfunctionCode;
//菜单码
privateStringparentFunctionCode;
//父级菜单码
privateStringfunctionName;
//菜单名
privateBooleanflag;
//true:选中false:未选中
privateListchildrenList;
publicStringgetFunctionCode(){
returnfunctionCode;
}
publicvoidsetFunctionCode(StringfunctionCode){
this.functionCode=functionCode;
}
publicStringgetParentFunctionCode(){
returnparentFunctionCode;
}
publicvoidsetParentFunctionCode(StringparentFunctionCode){
this.parentFunctionCode=parentFunctionCode;
}
publicStringgetFunctionName(){
returnfunctionName;
}
publicvoidsetFunctionName(StringfunctionName){
this.functionName=functionName;
}
publicBooleangetFlag(){
returnflag;
}
publicvoidsetFlag(Booleanflag){
this.flag=flag;
}
publicListgetChildrenList(){
returnchildrenList;
}
publicvoidsetChildrenList(ListchildrenList){
this.childrenList=childrenList;
}
}
3.递归工具类
packagecom.ieou.capsule.util;
importcom.ieou.capsule.dto.SystemPermissions.SystemPermissionsTree;
importjava.util.ArrayList;
importjava.util.List;
publicclassTreeUtil{
/**
*作者:一沐枫一
*来源:CSDN
*原文:https://blog.csdn.net/gxgl8811/article/details/72803833
*版权声明:本文为博主原创文章,转载请附上博文链接!
*/
publicstaticListgetTreeList(ListentityList){
ListresultList=newArrayList<>();
//获取顶层元素集合
StringparentCode;
for(SystemPermissionsTreeentity:entityList){
parentCode=entity.getParentFunctionCode();
//顶层元素的parentCode==null或者为0
if(parentCode==null||"0".equals(parentCode)){
resultList.add(entity);
}
}
//获取每个顶层元素的子数据集合
for(SystemPermissionsTreeentity:resultList){
entity.setChildrenList(getSubList(entity.getFunctionCode(),entityList));
}
returnresultList;
}
/**
*获取子数据集合
*
*@paramid
*@paramentityList
*@return
*@authorjianda
*@date2017年5月29日
*/
privatestaticListgetSubList(Stringid,ListentityList){
ListchildList=newArrayList<>();
StringparentId;
//子集的直接子对象
for(SystemPermissionsTreeentity:entityList){
parentId=entity.getParentFunctionCode();
if(id.equals(parentId)){
childList.add(entity);
}
}
//子集的间接子对象
for(SystemPermissionsTreeentity:childList){
entity.setChildrenList(getSubList(entity.getFunctionCode(),entityList));
}
//递归退出条件
if(childList.size()==0){
returnnull;
}
returnchildList;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。