当前位置:网站首页>Recursive menu query (recursion: check yourself)
Recursive menu query (recursion: check yourself)
2022-07-25 16:04:00 【Leon_ Jinhai_ Sun】
(1)level by 1 Namely 1 level ,children It is the submenu at this level
(2) Only existing data can be recursive
(3) Recursion requires an entry , That is, where to start
Concrete realization :
(1) Recursive query :
The complete code is as follows ( The analysis is as follows )
//======================== Recursively query all menus ===========
// Get all menus
@Override
public List<Permission> queryAllMenuGuli() {
//1 Query all data in the menu table
QueryWrapper<Permission> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
List<Permission> permissionList = baseMapper.selectList(wrapper);
//2 Query all menus list The collection is encapsulated as required
List<Permission> resultList = bulidPermission(permissionList);
return resultList;
}
// Return to all menus list A method of encapsulating a collection
public static List<Permission> bulidPermission(List<Permission> permissionList) {
// establish list aggregate , Used for data final encapsulation
List<Permission> finalNode = new ArrayList<>();
// Put all the menus list A collection of traverse , Get the top menu pid=0 menu , Set up level yes 1
for(Permission permissionNode : permissionList) {
// Get the top menu pid=0 menu
if("0".equals(permissionNode.getPid())) {
// Set the top-level menu level yes 1
permissionNode.setLevel(1);
// According to the top menu , Query the submenu , Package to finalNode Inside
//permissionNode This is level by 1 All data for
finalNode.add(selectChildren(permissionNode,permissionList));
}
}
return finalNode;
}
// This is to query the secondary and tertiary menus :
private static Permission selectChildren(Permission permissionNode, List<Permission> permissionList) {
//1 Because put the second level menu in the first level menu , There are three more floors inside the second floor , Initialize the object
permissionNode.setChildren(new ArrayList<Permission>());
//2 Traverse all menus list aggregate , Make judgments and compare , Compare id and pid Is the value the same
for(Permission it : permissionList) {
// Judge id and pid Is the value the same
if(permissionNode.getId().equals(it.getPid())) {
// Put the parent menu level value +1
int level = permissionNode.getLevel()+1;
it.setLevel(level);
// If children It's empty , Perform initialization operation
if(permissionNode.getChildren() == null) {
permissionNode.setChildren(new ArrayList<Permission>());
}
// Put the queried sub menu into the parent menu
permissionNode.getChildren().add(selectChildren(it,permissionList));
}
}
return permissionNode;
} //======================== Recursively query all menus
Get all the menus first , And then make a query
@Override
public List<Permission> queryAllMenuGuli() {
//1 Query all data in the menu table
QueryWrapper<Permission> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
List<Permission> All contents ( Unpacked ) = baseMapper.selectList(wrapper);
//2 Query all menus list The collection is encapsulated as required
List<Permission> resultList = bulidPermission( All contents ( Unpacked ));
return resultList;
}
// Return to all menus list A method of encapsulating a collection
public static List<Permission> bulidPermission(List<Permission> permissionList【 All contents ( Unpacked )】) {
// establish list aggregate , Used for data final encapsulation
List<Permission> finalNode 【 All contents ( Sealed )】= new ArrayList<>();
// Put all the menus list A collection of traverse , Get the top menu pid=0 menu , Set up level yes 1
for(Permission permissionNode【 First class menu 】 : permissionList) {
// Get the top menu pid=0 menu
if("0".equals(permissionNode.getPid())) {
// Set the top-level menu level yes 1
permissionNode.setLevel(1);
// According to the top menu , Query the submenu , Package to finalNode Inside
//permissionNode This is level by 1 All data for
finalNode All contents ( Sealed ).add(selectChildren( New method )(permissionNode【 First class menu 】,permissionList【 All contents ( Unpacked )】));-- This is to prepare for recursion
}
}
return finalNode;
}
// This is to query the secondary and tertiary menus :
private static Permission selectChildren(permissionNode【 First class menu 】,permissionList【 All contents ( Unpacked )】) {
//1 Because put the second level menu in the first level menu , There are three more floors inside the second floor , Initialize the object
permissionNode.setChildren(new ArrayList<Permission>());
//2 Traverse all menus list aggregate , Make judgments and compare , Compare id and pid Is the value the same
for(Permission it【 II. Contents of the menu 】 : permissionList【 All contents ( Unpacked )】) {
// Judge id and pid Is the value the same
if(permissionNode【 First class menu 】.getId().equals(it【 II. Contents of the menu 】.getPid())) {
// Put the parent menu level value +1
int level = permissionNode.getLevel()+1;
it.setLevel(level);
// If children It's empty , Perform initialization operation
if(permissionNode.getChildren() == null) {
permissionNode.setChildren(new ArrayList<Permission>());
}-- Add a beautiful thing to a contrasting beautiful thing , You can do without it
// Put the queried sub menu into the parent menu
permissionNode【 First class menu 】.getChildren().add(selectChildren(it【 II. Contents of the menu 】,permissionList【 All contents ( Unpacked )】));-- The next step is to cycle the above steps
}
}
return permissionNode【 First class menu 】;
}
边栏推荐
- Pytoch learning notes - Teacher Liu Er RNN advanced chapter - code comments and results
- 乐观锁悲观锁适用场景
- 墨天轮高分技术文档分享——数据库安全篇(共48个)
- 没错,请求DNS服务器还可以使用UDP协议
- Product upgrade observation station in June
- MySQL-自增锁
- Basic usage of MFC thread afxbeginthread, passing multiple parameters
- Leetcode - 641 design cycle double ended queue (Design)*
- 产品动态丨Android 13 高效适配全新升级
- 权限管理-角色分配菜单
猜你喜欢

狂神redis笔记12

How Google cloud disk is associated with Google colab

Leetcode - 677 key value mapping (Design)*

Matlab -- CVX optimization kit installation

ML - Speech - traditional speech model

CircleIndicator组件,使指示器风格更加多样化

电阻电路的等效变化(Ⅱ)

Leetcode - 303 area and retrieval - array immutable (design prefix and array)

【服务器数据恢复】HP EVA服务器存储意外断电导致RAID信息丢失的数据恢复案例

Leetcode - 641 design cycle double ended queue (Design)*
随机推荐
Reasons for data format conversion when matlab reads the displayed image
Leetcode - 362 knock counter (Design)
Simple rotation map and hamster beating
MySQL tutorial 66 data table query statement
Leetcode - 359 log rate limiter (Design)
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel
Boomi荣获“多元化最佳首席执行官奖”和“职业成长最佳公司奖”,在大型公司类别中跻身50强
Matlab -- CVX optimization kit installation
泰山OFFICE技术讲座:英寸,厘米,磅,派卡,提,行,字行,像素的换算关系
SVD singular value decomposition derivation and application and signal recovery
Pytoch framework exercise (based on kaggle Titanic competition)
没错,请求DNS服务器还可以使用UDP协议
Waterfall flow layout
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
MySQL-自增锁
[Shakespeare: keep the fun of being a man]
Pytoch learning notes -- Summary of common functions 3
30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)
MySQL教程66-数据表查询语句