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 中HashMap實(shí)現(xiàn)原理深入理解
這篇文章主要介紹了java 中HashMap實(shí)現(xiàn)原理深入理解的相關(guān)資料,需要的朋友可以參考下2017-03-03Spring?Security實(shí)現(xiàn)接口放通的方法詳解
在用Spring?Security項(xiàng)目開發(fā)中,有時(shí)候需要放通某一個(gè)接口時(shí),我們需要在配置中把接口地址配置上,這樣做有時(shí)候顯得麻煩。本文將通過一個(gè)注解的方式快速實(shí)現(xiàn)接口放通,感興趣的可以了解一下2022-05-05分別在Groovy和Java中創(chuàng)建并初始化映射的不同分析
這篇文章主要為大家介紹了分別在Groovy和Java中創(chuàng)建并初始化映射的不同分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03java中Base64字符串出現(xiàn)不合法字符的問題解決
非法的base64數(shù)據(jù)可能導(dǎo)致編碼或解碼過程出錯(cuò),本文主要介紹了java中Base64字符串出現(xiàn)不合法字符的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06淺析非對稱加密在接口參數(shù)中的實(shí)現(xiàn)
接口層做數(shù)據(jù)加密應(yīng)該算是老生常談的一件事了,業(yè)界用的比較多的,不外乎是對稱加密,非對稱加密以及兩者的結(jié)合。本文就來聊聊非對稱加密在接口參數(shù)中的實(shí)現(xiàn),希望對大家有所幫助2023-02-02