Java查询菜单树形结构-树形菜单组装数据

话不多说直接上干货


public List<MenuTree> queryTree() {
    MenuExample example = new MenuExample();
    example.setOrderByClause("seq ASC");
    List<Menu> menuList = menuMapper.selectByExample(example);
    Integer parentId = 0;
    //递归思想,查找所有的子菜单树
    return this.build(menuList, parentId);
}

//递归查子菜单
private List<MenuTree> build(List<Menu> menuList, Integer parentId) {
    return menuList.stream().filter(menu -> menu.getParentId().intValue() == parentId.intValue()).map(menu -> {
        MenuTree menuTree = BeansUtil.convert(menu, MenuTree.class);
        List<MenuTree> childList = build(menuList, menuTree.getId());
        if (childList != null && childList.size() > 0) {
            menuTree.setChildren(childList);
        }
        return menuTree;
    }).collect(Collectors.toList());
}

结果

{
  "code": "0",
  "msg": "",
  "data": [
    {
      "id": 10,
      "name": "首页",
      "icon": "icon",
      "path": "/index",
      "types": "Link",
      "parentId": 0,
      "seq": 1
    },
    {
      "id": 11,
      "name": "菜单1",
      "icon": "icon",
      "path": "/m1",
      "types": "Folder",
      "parentId": 0,
      "seq": 2,
      "children": [
        {
          "id": 1101,
          "name": "菜单1子菜单1",
          "icon": "icon",
          "path": "/m1/m1",
          "types": "Folder",
          "parentId": 11,
          "seq": 1,
          "children": [
            {
              "id": 110101,
              "name": "菜单1子菜单1页面1",
              "icon": "icon",
              "path": "/m1/m1/p1",
              "types": "Link",
              "parentId": 1101,
              "seq": 1
            },
            {
              "id": 110102,
              "name": "菜单1子菜单1页面2",
              "icon": "icon",
              "path": "/m1/m1/p2",
              "types": "Link",
              "parentId": 1101,
              "seq": 2
            },
            {
              "id": 110103,
              "name": "菜单1子菜单1页面3",
              "icon": "icon",
              "path": "/m1/m1/p3",
              "types": "Link",
              "parentId": 1101,
              "seq": 3
            }
          ]
        },
        {
          "id": 1102,
          "name": "菜单1子菜单2",
          "icon": "icon",
          "path": "/m1/m2",
          "types": "Folder",
          "parentId": 11,
          "seq": 2,
          "children": [
            {
              "id": 110201,
              "name": "菜单1子菜单2页面1",
              "icon": "icon",
              "path": "/m1/m2/p1",
              "types": "Link",
              "parentId": 1102,
              "seq": 1
            },
            {
              "id": 110202,
              "name": "菜单1子菜单2页面2",
              "icon": "icon",
              "path": "/m1/m2/p2",
              "types": "Link",
              "parentId": 1102,
              "seq": 2
            }
          ]
        }
      ]
    },
    {
      "id": 12,
      "name": "菜单2",
      "icon": "icon",
      "path": "/m2",
      "types": "Folder",
      "parentId": 0,
      "seq": 3,
      "children": [
        {
          "id": 1201,
          "name": "菜单2页面1",
          "icon": "icon",
          "path": "/m2/p1",
          "types": "Link",
          "parentId": 12,
          "seq": 1
        },
        {
          "id": 1202,
          "name": "菜单2页面2",
          "icon": "icon",
          "path": "/m2/p2",
          "types": "Link",
          "parentId": 12,
          "seq": 2
        }
      ]
    },
    {
      "id": 13,
      "name": "菜单3",
      "icon": "icon",
      "path": "/m3",
      "types": "Folder",
      "parentId": 0,
      "seq": 4,
      "children": [
        {
          "id": 1301,
          "name": "菜单3页面1",
          "icon": "icon",
          "path": "/m3/p1",
          "types": "Link",
          "parentId": 13,
          "seq": 1
        }
      ]
    }
  ],
  "times": 1577683755976
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值