递归查询显示列表数据

返回的数据格式如下

 "status": 1,
    "info": "请求成功",
    -"data": [
    -{
        "menuId": 1,
        "parentId": 0,
        "name": "系统管理",
        "url": null,
        "perms": null,
        "type": 0,
        "icon": "system",
        "orderNum": 0,
        "roleId": 1,
        "sMenuId": 1,
        -"childList": [
        -{
                 "menuId": 2,
                 "parentId": 1,
                 "name": "管理员列表",
                 "url": "sys/user",
                "perms": null,
                  "type": 1,
                 "icon": "admin",
                 "orderNum": 1,
                "roleId": 1,
                "sMenuId": 2,
              -"childList": [
            -{
                       "menuId": 8,
                       "parentId": 2,
                       "name": "新增",
                       "url": null,
                      "perms": "sys:schedule:save",
                       "type": 2,
                       "icon": null,
                        "orderNum": 0,
                        "roleId": 1,
                         "sMenuId": 8,
                         "childList": null
            }
        ]
    }

实现代码

//层级获取菜单
public List<MenuEntity> hierarchicalAccessMenu(String roleId){
    //获取该角色下所有菜单
    List<MenuEntity> menuList = menuDao.getMenuListByRole(roleId);
 
    //找到所有的1级菜单
    List<MenuEntity> mList = new ArrayList<>();
    for (int i = 0; i < menuList.size(); i++) {
        if (menuList.get(i).getParentId()==0){
            mList.add(menuList.get(i));
        }
    }
    //为一级菜单设置子菜单准备递归
    for (MenuEntity menu:mList) {
        //获取父菜单下所有子菜单调用getChildList
        List<MenuEntity> childList = getChildList(String.valueOf(menu.getMenuId()),menuList);
        menu.setChildList(childList);
    }
    return mList;
}



public List<MenuEntity> getChildList(String id,List<MenuEntity> menuList){
    //构建子菜单
    List<MenuEntity> childList = new ArrayList<>();
    //遍历传入的list
    for (MenuEntity menu:menuList) {
        //所有菜单的父id与传入的根节点id比较,若相等则为该级菜单的子菜单
        if (String.valueOf(menu.getParentId()).equals(id)){
            childList.add(menu);
        }
    }
    //递归
    for (MenuEntity m:childList) {
        m.setChildList(getChildList(String.valueOf(m.getMenuId()),menuList));
    }
    if (childList.size() == 0){
        return null;
    }
    return childList;
}



原文链接:https://blog.csdn.net/GayFei/article/details/81262904

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值