当前位置:网站首页>Realize multi-level linkage through recursion
Realize multi-level linkage through recursion
2022-07-29 03:26:00 【Fairy wants carry】
Get all menus
our id Is the current menu ,pid Is the parent menu

Ideas :
First Get all the menu values and package them into the collection , Then all menu data To the method of constructing multi-level menus , In the construction method of multi-level menu, we First, determine the final returned set ( In fact, it is a parent menu node ), Then traverse all menus Find the parent menu of the root node , Set the... Of the submenu level Hierarchy , and According to the top-level menu, all menu data is encapsulated into the final set
The main method is to add submenus to the main node , And return to the primary node : First, you need to set in the master node children aggregate , Then traverse all the data , Find the menu corresponding to the parent dish , And set the level level, Determine the parent menu children by null after , Add the sub menu that meets the conditions to the parent ( Here recursion , The sub menu becomes a local parent menu )
Get all menus with list Return because the parent menu will be added later , So we have to use list, It's not good to return directly to the parent menu , It's hard to find ideas , Of course, maybe
/**
* 1. Get all menus
* @return
*/
@Override
public List<Permission> queryAllMenu() {
//1.wrapper Encapsulation condition
QueryWrapper<Permission> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
//2. according to wrapper The condition returns data
List<Permission> permissionList = baseMapper.selectList(wrapper);
//3. Divide these data into menus , Save as a collection
List<Permission> result=build(permissionList);
return result;
}
/**
* 2. Use the recursive method to build our data into menus ( Here is to filter out the master node , And call the method of adding submenus )
* @param treeNodes: All the data
* @return
*/
private static List<Permission> build(List<Permission> treeNodes){
// As the final data package
ArrayList<Permission> trees = new ArrayList<>();
//1. Traverse all menus , Find the root node , And add submenus
for (Permission treeNode : treeNodes) {
if("0".equals(treeNode.getPid())){
treeNode.setLevel(1);// Set the level of the root node
trees.add(findChildren(treeNode,treeNodes));// According to the top menu , The query submenu is encapsulated in trees in
}
}
return trees;
}
/**
* 3. Add a submenu to the main node , And return to the primary node
* @param p_node: Master node ( It can be understood as the front menu )
* @param nodedata: All the data
* @return
*/
private static Permission findChildren(Permission p_node,List<Permission> nodedata){
//1. First, set it up on the high floor , That is, the low-level menu
p_node.setChildren(new ArrayList<Permission>());
//2. Traverse all menu data , Compare id and pid Are they the same? , The same description is subordination
for (Permission n_node : nodedata) {
if(n_node.getPid().equals(p_node.getId())){
//2.1 Set parent menu level+1 Set to submenu
int level = p_node.getLevel() + 1;
n_node.setLevel(level);
//3. If the parent menu children Null, initialization required
if(p_node.getChildren()==null){
p_node.setChildren(new ArrayList<Permission>());
}
//4. Add the currently qualified submenu to the parent ( Here recursion ,n_node Become the parent menu )
p_node.getChildren().add(findChildren(n_node,nodedata));
}
}
return p_node;
}Recursive delete
It's very simple , It is also a sub menu of the current parent menu , Then recurse the submenu , be-all id Add to collection , Last delete Line delete
/**
* 4. Recursively delete menu
* @param id
*/
@Override
public void removeChildById(String id) {
//1. Create a collection to encapsulate all deleted id
ArrayList<String> idList = new ArrayList<>();
//2. Add deleted menu id value
this.selectPermissionByChildId(id,idList);
//3. Later, you need to change the current menu id encapsulate , If you do not delete the current top-level menu, you can not delete
idList.add(id);
//4. Finally, delete
baseMapper.deleteBatchIds(idList);
}
/**
* 5. According to the current menu id, Sub menus in the query menu , Then encapsulate the list aggregate
* @param id
* @param idList: Storage deleted id
*/
private void selectPermissionByChildId(String id, List<String> idList) {
//1. Query all menus (id), The condition is the parent menu of the submenu pid The value is id
QueryWrapper<Permission> wrapper = new QueryWrapper<>();
wrapper.eq("pid",id);
wrapper.select("id");
List<Permission> childIdList = baseMapper.selectList(wrapper);
//2. Then, select id It's worth taking out , Encapsulate into a collection idList in , Because the submenu is the parent menu for its submenu , So here is recursion
childIdList.stream().forEach(item->{
//2.1 Current submenu id Package to idList in
idList.add(item.getId());
//2.2 Recursively query the current submenu
this.selectPermissionByChildId(item.getId(),idList);
});
}边栏推荐
- Introduction to JVM foundation I (memory structure)
- Idea configuration web container and war packaging
- MYCAT read / write separation configuration
- Military product development process - transition phase
- Summary of SAP localized content in China
- Regular expression bypasses WAF
- 再学EXKMP(EXKMP模板)
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- Bingbing learning notes: operator overloading -- implementation of date class
- 13_ UE4 advanced_ Montage animation realizes attack while walking
猜你喜欢

基于单片机烟雾温湿度甲醛监测设计
![[freeswitch development practice] unimrcp compilation and installation](/img/ef/b82326152326293bf98e89da28b887.png)
[freeswitch development practice] unimrcp compilation and installation

ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?

July 28, 2022 Gu Yujia's study notes

Let's talk about the summary of single merchant function modules

逐步分析类的拆分之案例——五彩斑斓的小球碰撞

AI_ Drug: VAE of molecular generation model (I)

Introduction to JVM foundation I (memory structure)

美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢

简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?
随机推荐
ShardingSphere之水平分表实战(三)
Asynchronous callback future mode of concurrent mode
Matlab learning -- structured programs and user-defined functions
Anti vulnerability · benefit from uncertainty --- management?
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Redis之sentinel哨兵集群怎么部署
Let's talk about the summary of single merchant function modules
暴力递归到动态规划 01 (机器人移动)
Bingbing learning notes: operator overloading -- implementation of date class
Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer
Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
Shell script summary
简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?
深入C语言(4)——switch的定义与使用
GJB common confused concepts
再学EXKMP(EXKMP模板)
Arm architecture and neural network
Singleton and invariant modes of concurrent mode
Digital image processing Chapter 10 - image segmentation
Makefile details