java實現(xiàn)菜單樹的示例代碼
更新時間:2023年12月27日 16:39:45 作者:LonesomeRoad
這篇文章主要為大家詳細介紹了java實現(xiàn)菜單樹的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下
使用ruoyi的菜單表結構

實體類增加注解

實體類增加子菜單數(shù)組
@TableField(exist = false)
private List<Menu> children;
實現(xiàn)邏輯
public List<Menu> selectMenuList(String menuName, Long userId) {
//樹結構
List<Menu> menuList = null;
LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(checkIsAdmin.checkIsAdmin(principal.getUser().getId())){
LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(menuName),Menu::getMenuName, menuName);
menuList = this.menuMapper.selectList(queryWrapper);
}else{
menuList = this.menuMapper.selectMenuListByUserId(menuName,userId);
}
List<Menu> finalMenuList = menuList;
return menuList.stream()
.filter(item -> Objects.equals(item.getParentId().toString(),"0"))
.map(item -> item.setChildren(getChild(item.getId(), finalMenuList)))
.sorted(Comparator.comparingInt(menu -> (menu.getShowSort() == null ? 0 : menu.getShowSort())))
.collect(Collectors.toList());
}
private List<Menu> getChild(Long id, List<Menu> menuList){
return menuList.stream()
.filter(item -> Objects.equals(item.getParentId().toString(), id.toString()))
.map(item -> item.setChildren(getChild(item.getId(), menuList)))
.sorted(Comparator.comparingInt(menu -> (menu.getShowSort() == null ? 0 : menu.getShowSort())))
.collect(Collectors.toList());
}結果展示
{
"code": 200,
"msg": "操作成功",
"data": [
{
"id": "1731872513806221314",
"menuName": "系統(tǒng)管理",
"parentId": 0,
"showSort": 1,
"url": null,
"reqPath": null,
"isCache": "1",
"target": null,
"menuType": "M",
"visible": "0",
"isRefresh": null,
"perms": null,
"icon": "ep:add-location",
"createTime": "2023-12-05 11:05:58",
"updateTime": null,
"operater": "qinyi",
"componentName": null,
"children": [
{
"id": "1731936951137632258",
"menuName": "角色管理",
"parentId": "1731872513806221314",
"showSort": 1,
"url": null,
"reqPath": null,
"isCache": "1",
"target": null,
"menuType": "C",
"visible": "0",
"isRefresh": null,
"perms": null,
"icon": "ep:alarm-clock",
"createTime": "2023-12-05 15:22:01",
"updateTime": null,
"operater": "qinyi",
"componentName": null,
"children": []
},
{
"id": "1734381007881097218",
"menuName": "角色管理222",
"parentId": "1731872513806221314",
"showSort": 2,
"url": null,
"reqPath": null,
"isCache": "1",
"target": null,
"menuType": "C",
"visible": "0",
"isRefresh": null,
"perms": null,
"icon": "ep:apple",
"createTime": "2023-12-12 09:13:50",
"updateTime": null,
"operater": "qinyi",
"componentName": null,
"children": [
{
"id": "1734768479693627394",
"menuName": "新增按鈕",
"parentId": "1734381007881097218",
"showSort": 1,
"url": "",
"reqPath": "",
"isCache": "1",
"target": null,
"menuType": "F",
"visible": "0",
"isRefresh": null,
"perms": null,
"icon": "",
"createTime": "2023-12-13 10:53:30",
"updateTime": null,
"operater": "qinyi",
"componentName": null,
"children": []
}
]
}
]
},
{
"id": "1732203279467573249",
"menuName": "菜單管理哦",
"parentId": 0,
"showSort": 2,
"url": null,
"reqPath": null,
"isCache": "1",
"target": null,
"menuType": "C",
"visible": "0",
"isRefresh": null,
"perms": null,
"icon": "ep:camera-filled",
"createTime": "2023-12-06 09:00:19",
"updateTime": null,
"operater": "qinyi",
"componentName": null,
"children": []
}
]
}這樣寫有點問題,就是模糊查詢的時候,查不到數(shù)據(jù),也就是過濾數(shù)據(jù)的時候子節(jié)點加載不到完整的樹結構,做以下改動

/**
* 在樹結構上做模糊查詢(剪枝操作)
*/
private List<Menu> getMenuByName(List<Menu> menuList,String selectName){
Iterator<Menu> iterator = menuList.iterator();
while(iterator.hasNext()){
Menu menu = iterator.next();
if(!menu.getMenuName().contains(selectName)){
List<Menu> childrenList = menu.getChildren();
if(!CollectionUtils.isEmpty(childrenList)){
getMenuByName(childrenList, selectName);
}
if(CollectionUtils.isEmpty(childrenList)){
iterator.remove();
}
}
}
return menuList;
}以上就是java實現(xiàn)菜單樹的示例代碼的詳細內(nèi)容,更多關于java菜單樹的資料請關注腳本之家其它相關文章!
相關文章
Spring?Security實現(xiàn)接口放通的方法詳解
在用Spring?Security項目開發(fā)中,有時候需要放通某一個接口時,我們需要在配置中把接口地址配置上,這樣做有時候顯得麻煩。本文將通過一個注解的方式快速實現(xiàn)接口放通,感興趣的可以了解一下2022-05-05
分別在Groovy和Java中創(chuàng)建并初始化映射的不同分析
這篇文章主要為大家介紹了分別在Groovy和Java中創(chuàng)建并初始化映射的不同分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03
java中Base64字符串出現(xiàn)不合法字符的問題解決
非法的base64數(shù)據(jù)可能導致編碼或解碼過程出錯,本文主要介紹了java中Base64字符串出現(xiàn)不合法字符的問題解決,具有一定的參考價值,感興趣的可以了解一下2024-06-06

