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;
    }

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值