java实现菜单树

2023-12-18 12:54:40

使用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": []
    }
  ]
}

文章来源:https://blog.csdn.net/qq_24138151/article/details/135058988
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。