当前位置:网站首页>通过递归实现多级联动
通过递归实现多级联动
2022-07-29 03:25:00 【Fairy要carry】
获取全部菜单
我们的id是当前菜单,pid是父菜单

思路:
首先获取所有菜单值封装到集合中,然后将所有菜单数据传到构造多级菜单的方法中,在多级菜单的构造方法中我们首先确定最终返回的集合(其实也就是一个父亲菜单节点),然后遍历所有菜单找到根节点父菜单,设置子菜单的level层级,并根据顶层菜单利用所有菜单数据封装到最终集合中
主要是子菜单添加到主节点这个方法,并且返回主节点:首先需要在主节点设置children集合,然后遍历所有数据,找到与父菜单相对应的菜单,并且设置层级level,判断父菜单children为null后,将满足条件的子菜单加入父中(这里递归,子菜单变为局部父菜单)
获取全部菜单用list返回是因为后面还是要添加父菜单的,所以得用list,不好直接返回父菜单,思路不好找,当然也许
/**
* 1.获取全部菜单
* @return
*/
@Override
public List<Permission> queryAllMenu() {
//1.wrapper封装条件
QueryWrapper<Permission> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
//2.根据wrapper条件返回数据
List<Permission> permissionList = baseMapper.selectList(wrapper);
//3.将这些数据分为菜单,保存为一个集合
List<Permission> result=build(permissionList);
return result;
}
/**
* 2.使用递归方法将我们的数据建为菜单(这里是筛选出主节点,并且调用添加子菜单的方法)
* @param treeNodes:所有数据
* @return
*/
private static List<Permission> build(List<Permission> treeNodes){
//作为最终的数据封装
ArrayList<Permission> trees = new ArrayList<>();
//1.遍历所有菜单,找到根节点,并添加子菜单
for (Permission treeNode : treeNodes) {
if("0".equals(treeNode.getPid())){
treeNode.setLevel(1);//设置根节点的层级
trees.add(findChildren(treeNode,treeNodes));//根据顶层菜单,查询子菜单封装到trees中
}
}
return trees;
}
/**
* 3.将子菜单添加到主节点中,并且返回主节点
* @param p_node:主节点(可以理解为前层菜单)
* @param nodedata:所有数据
* @return
*/
private static Permission findChildren(Permission p_node,List<Permission> nodedata){
//1.首先在高层放集合,也就是低层菜单
p_node.setChildren(new ArrayList<Permission>());
//2.向所有菜单数据进行遍历,进行比较id和pid是否相同,相同说明是从属关系
for (Permission n_node : nodedata) {
if(n_node.getPid().equals(p_node.getId())){
//2.1将父菜单level+1设置到子菜单中
int level = p_node.getLevel() + 1;
n_node.setLevel(level);
//3.如果父菜单的children为空需要初始化
if(p_node.getChildren()==null){
p_node.setChildren(new ArrayList<Permission>());
}
//4.将当前满足条件的子菜单添加到父中(这里递归,n_node变为父菜单)
p_node.getChildren().add(findChildren(n_node,nodedata));
}
}
return p_node;
}递归删除
其实很简单,也是找当前父菜单的子菜单,然后对子菜单进行递归就好了,所有的id添加到集合中,最后delete行删除
/**
* 4.递归删除菜单
* @param id
*/
@Override
public void removeChildById(String id) {
//1.创建一个集合封装所有被删id
ArrayList<String> idList = new ArrayList<>();
//2.添加被删菜单的id值
this.selectPermissionByChildId(id,idList);
//3.后面还需要将当前菜单id进行封装,如果不删当前最高级菜单可以不删除
idList.add(id);
//4.最后进行删除
baseMapper.deleteBatchIds(idList);
}
/**
* 5.根据当前菜单id,查询菜单里面的子菜单,然后封装到list集合
* @param id
* @param idList:存储被删id
*/
private void selectPermissionByChildId(String id, List<String> idList) {
//1.查询所有菜单(id),条件是子菜单的父菜单pid值为id
QueryWrapper<Permission> wrapper = new QueryWrapper<>();
wrapper.eq("pid",id);
wrapper.select("id");
List<Permission> childIdList = baseMapper.selectList(wrapper);
//2.然后将子菜单中的id值取出来,封装到集合idList中,因为子菜单对于它的子菜单为父菜单,所以这里进行递归
childIdList.stream().forEach(item->{
//2.1当前子菜单id封装到idList中
idList.add(item.getId());
//2.2对当前子菜单进行递归查询
this.selectPermissionByChildId(item.getId(),idList);
});
}边栏推荐
- During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“
- Flask creation process day05-06 creation project
- Detailed steps for installing MySQL 8.0 under Linux
- 军品研制过程-转阶段
- Digital image processing Chapter 10 - image segmentation
- C and pointer Chapter 3 semantic "trap" 3.5 null pointer is not a string
- Principle knowledge is useful
- 2 neural network toolbox NN
- 正则表达绕过waf
- xxxxx
猜你喜欢

Producer consumer model of concurrent model

Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it

3D advanced renderer: artlandis studio 2021.2 Chinese version

Sleuth+Zipkin 来进行分布式服务链路的追踪

年内首个“三连跌” 95号汽油回归“8元时代“

12_ UE4 advanced_ Change a more beautiful character model

Multi level wavelet CNN for image restoration

NXP i.mx8mp-deepviewrt

The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled

Singleton mode (hungry and lazy)
随机推荐
[freeswitch development practice] media bug obtains call voice flow
RTP 发送 和接收 h265
Shortcut key for adjusting terminal size in ROS
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
Idea configuration web container and war packaging
[technology 1]
MYCAT read / write separation configuration
逐步分析类的拆分之案例——五彩斑斓的小球碰撞
Three military product baselines (functional baseline, distribution baseline, product baseline) and the documents contained in the baseline
July 28, 2022 Gu Yujia's study notes
Watermelon book learning Chapter 6 -- SVM
Tonight at 7:30 | is the AI world in the eyes of Lianjie, Jiangmen, Baidu and country garden venture capital continue to be advanced or return to the essence of business
1.5 nn. Module neural network (III)
增量实时灾备笔记
"PHP Basics" output approximate value of PI
shell脚本总结
Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind
Shell script summary
Minesweeping simple version
How close can QA be to business code Direct exposure of defects through codediff