java實(shí)現(xiàn)菜單樹的示例代碼
使用ruoyi的菜單表結(jié)構(gòu)

實(shí)體類增加注解

實(shí)體類增加子菜單數(shù)組
@TableField(exist = false)
private List<Menu> children;
實(shí)現(xiàn)邏輯
public List<Menu> selectMenuList(String menuName, Long userId) {
//樹結(jié)構(gòu)
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());
}結(jié)果展示
{
"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": []
}
]
}這樣寫有點(diǎn)問題,就是模糊查詢的時(shí)候,查不到數(shù)據(jù),也就是過濾數(shù)據(jù)的時(shí)候子節(jié)點(diǎn)加載不到完整的樹結(jié)構(gòu),做以下改動(dòng)

/**
* 在樹結(jié)構(gòu)上做模糊查詢(剪枝操作)
*/
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實(shí)現(xiàn)菜單樹的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于java菜單樹的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java如何用Processing生成馬賽克風(fēng)格的圖像
這篇文章主要介紹了如何用java如何用Processing生成馬賽克風(fēng)格的圖像,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決
這篇文章主要介紹了springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題
當(dāng)進(jìn)行業(yè)務(wù)操作時(shí),訂單發(fā)生異常 ,進(jìn)行了回滾操作,因?yàn)樵诓煌臄?shù)據(jù)庫實(shí)例中,余額卻扣除成功,此時(shí)發(fā)現(xiàn)數(shù)據(jù)不一致問題,本文給大家介紹Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題,感興趣的朋友一起看看吧2023-11-11
java?數(shù)組越界判斷和獲取數(shù)組長度的實(shí)現(xiàn)方式
這篇文章主要介紹了java?數(shù)組越界判斷和獲取數(shù)組長度的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的全過程
我們開發(fā)業(yè)務(wù)系統(tǒng)的時(shí)候,經(jīng)常有那種文檔文件在線預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2021-11-11
String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法
今天小編就為大家分享一篇String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

