当前位置:网站首页>通过递归实现多级联动
通过递归实现多级联动
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);
});
}
边栏推荐
- Swing V2: towards a larger model with larger capacity and higher resolution
- 【科技1】
- Learn exkmp again (exkmp template)
- Rongyun IM & RTC capabilities on new sites
- Incremental real-time disaster recovery notes
- Introduction and advanced MySQL (XIV)
- "PHP Basics" output approximate value of PI
- STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
- Detailed steps for installing MySQL 8.0 under Linux
- 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
猜你喜欢
Producer consumer model of concurrent model
Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind
Makefile details
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
Digital image processing Chapter 10 - image segmentation
Example analysis of while, repeat and loop loops in MySQL process control
Ten thousand words detailed Google play online application standard package format AAB
STC单片机驱动1.8‘TFT SPI屏幕演示示例(含资料包)
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled
随机推荐
Reproduce 20 character short domain name bypass and XSS related knowledge points
腾讯云使用pem登录
简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?
Incremental real-time disaster recovery notes
Sleuth+Zipkin 来进行分布式服务链路的追踪
12_ UE4 advanced_ Change a more beautiful character model
Digital image processing Chapter 10 - image segmentation
C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
Asynchronous callback future mode of concurrent mode
服务器运行管理制度
A case of gradually analyzing the splitting of classes -- colorful ball collisions
KNN method predicts pregnancy, KNN principle simple code
Division and description of military technical documents
How to deploy sentinel cluster of redis
Arm architecture and neural network
Introduction and advanced MySQL (13)
Singleton mode (hungry and lazy)
Mathematical modeling -- analytic hierarchy process model
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
Precautions for using latex