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
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现遍历菜单可以使用递归的方式来操作。 首先,我们需要定义一个菜单的节点类,包含节点的名称和它的子节点列表。例如: ```java class TreeNode { private String name; private List<TreeNode> children; public TreeNode(String name) { this.name = name; this.children = new ArrayList<>(); } public String getName() { return name; } public void addChild(TreeNode child) { children.add(child); } public List<TreeNode> getChildren() { return children; } } ``` 然后,我们可以定义一个方法来遍历这个菜单: ```java public void traverseMenu(TreeNode menu) { if (menu == null) return; // 打印当前节点的名称 System.out.println(menu.getName()); // 遍历子节点 List<TreeNode> children = menu.getChildren(); for (TreeNode child : children) { traverseMenu(child); } } ``` 这个方法首先会打印当前节点的名称,然后遍历它的子节点,对每个子节点递归调用这个方法,实现对整个菜单的遍历。 使用示例: ```java public static void main(String[] args) { TreeNode root = new TreeNode("菜单"); TreeNode node1 = new TreeNode("节点1"); TreeNode node2 = new TreeNode("节点2"); TreeNode node3 = new TreeNode("节点3"); TreeNode node11 = new TreeNode("节点11"); TreeNode node12 = new TreeNode("节点12"); node1.addChild(node11); node1.addChild(node12); root.addChild(node1); root.addChild(node2); root.addChild(node3); traverseMenu(root); } ``` 输出结果: ``` 菜单 节点1 节点11 节点12 节点2 节点3 ``` 这样,我们就成功地使用Java实现了遍历菜单的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值