java实现菜单树的示例代码
作者:LonesomeRoad
这篇文章主要为大家详细介绍了java实现菜单树的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
使用ruoyi的菜单表结构
实体类增加注解
实体类增加子菜单数组
@TableField(exist = false) private List<Menu> children;
实现逻辑
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": "系统管理", "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": [] } ] }
这样写有点问题,就是模糊查询的时候,查不到数据,也就是过滤数据的时候子节点加载不到完整的树结构,做以下改动
/** * 在树结构上做模糊查询(剪枝操作) */ 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实现菜单树的示例代码的详细内容,更多关于java菜单树的资料请关注脚本之家其它相关文章!