说明

1.纯算法版本,跟treeListMap版本比起来的优点是,纯算法版只查询了一次数据库

2.里面有不同角色分配不同的菜单相关代码,注意复制粘贴。

3.可以无限嵌套层次结构,这里只用到了两层,仅为举例

先看效果

超级管理员结果展示

{
    "code": 200,
    "success": true,
    "msg": "获取成功",
    "sign": "",
    "result": [
        {
            "id": "59664367-11eb-489e-867d-af6c66129d03",
            "parentId": "0",
            "level": 1,
            "sort": 1,
            "type": 2,
            "path": "/Home/Introduce",
            "title": "首页",
            "icon": "home",
            "children": []
        },
        {
            "id": "30633357-dd12-415e-8631-660e2aa6b9ad",
            "parentId": "",
            "level": 1,
            "sort": 2,
            "type": 2,
            "path": null,
            "title": "信息管理",
            "icon": "home",
            "children": [
                {
                    "id": "e50cadbc-8b2b-4c8f-a262-9da95d3371bb",
                    "parentId": "30633357-dd12-415e-8631-660e2aa6b9ad",
                    "level": 2,
                    "sort": 1,
                    "type": 2,
                    "path": "/InfoManage/SoftManage/List",
                    "title": "软件管理",
                    "icon": "",
                    "children": null
                },
                {
                    "id": "94beb8ae-a606-4dea-bfa8-57bc734818bf",
                    "parentId": "30633357-dd12-415e-8631-660e2aa6b9ad",
                    "level": 2,
                    "sort": 2,
                    "type": 2,
                    "path": "/InfoManage/VersionsManage/List",
                    "title": "版本管理",
                    "icon": "",
                    "children": null
                },
                {
                    "id": "bcb68a35-eb0f-4696-acd8-3c76897e0f0f",
                    "parentId": "30633357-dd12-415e-8631-660e2aa6b9ad",
                    "level": 2,
                    "sort": 3,
                    "type": 2,
                    "path": "/InfoManage/UserManage/List",
                    "title": "用户管理",
                    "icon": "",
                    "children": null
                }
            ]
        },
        {
            "id": "d6321208-4980-46e3-b3d4-ec057009472c",
            "parentId": "0",
            "level": 1,
            "sort": 3,
            "type": 1,
            "path": null,
            "title": "数据维护",
            "icon": "home",
            "children": [
                {
                    "id": "b89eb2d8-40e4-4698-ab5a-ddaed7846ee1",
                    "parentId": "d6321208-4980-46e3-b3d4-ec057009472c",
                    "level": 2,
                    "sort": 1,
                    "type": 2,
                    "path": "/DataMaintenance/MsgManage/List",
                    "title": "回复管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "e70b3176-a2fa-4fc5-90f3-5abd9e2d13b8",
                    "parentId": "d6321208-4980-46e3-b3d4-ec057009472c",
                    "level": 2,
                    "sort": 2,
                    "type": 2,
                    "path": "/DataMaintenance/CardPassManage/List",
                    "title": "卡密管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "1d3838df-bc61-42e1-a149-dcc2705e2894",
                    "parentId": "d6321208-4980-46e3-b3d4-ec057009472c",
                    "level": 2,
                    "sort": 3,
                    "type": 2,
                    "path": "/DataMaintenance/FuncManage/List",
                    "title": "函数管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "5b35fe6a-da5f-46d5-b1ed-076308a38a13",
                    "parentId": "d6321208-4980-46e3-b3d4-ec057009472c",
                    "level": 2,
                    "sort": 4,
                    "type": 2,
                    "path": "/DataMaintenance/EventManage/List",
                    "title": "事件管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "a59b674e-8d45-46c2-be6e-ff44d2ee7b86",
                    "parentId": "d6321208-4980-46e3-b3d4-ec057009472c",
                    "level": 2,
                    "sort": 5,
                    "type": 2,
                    "path": "/DataMaintenance/BannedManage/List",
                    "title": "封禁管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "dbe9effe-0dc8-4378-a64c-5bf904ac6fae",
                    "parentId": "d6321208-4980-46e3-b3d4-ec057009472c",
                    "level": 2,
                    "sort": 6,
                    "type": 2,
                    "path": "/DataMaintenance/DataManage/List",
                    "title": "数据管理",
                    "icon": null,
                    "children": null
                }
            ]
        },
        {
            "id": "43767678-01c5-4b54-bfac-e108b6ceb32a",
            "parentId": "0",
            "level": 1,
            "sort": 4,
            "type": 1,
            "path": null,
            "title": "系统管理",
            "icon": "home",
            "children": [
                {
                    "id": "d7720078-3b65-4f83-b404-c117e5c46b68",
                    "parentId": "43767678-01c5-4b54-bfac-e108b6ceb32a",
                    "level": 2,
                    "sort": 1,
                    "type": 2,
                    "path": "/InfoManage/AdminManage/List",
                    "title": "管理员管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "9d953050-f354-4e9f-9f15-b91ea6a666f0",
                    "parentId": "43767678-01c5-4b54-bfac-e108b6ceb32a",
                    "level": 2,
                    "sort": 2,
                    "type": 2,
                    "path": "/asd",
                    "title": "管理员日志",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "71eedb8d-ae9e-4958-a351-c8dfb4711f21",
                    "parentId": "43767678-01c5-4b54-bfac-e108b6ceb32a",
                    "level": 2,
                    "sort": 3,
                    "type": 2,
                    "path": "/DataMaintenance/LogManage/List",
                    "title": "用户日志",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "546ea73a-1d46-41f5-bfa6-e646b3e741ae",
                    "parentId": "43767678-01c5-4b54-bfac-e108b6ceb32a",
                    "level": 2,
                    "sort": 4,
                    "type": 2,
                    "path": "/asd",
                    "title": "菜单管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "d58b4547-e20f-4eb9-b7d5-b3195ed5cc18",
                    "parentId": "43767678-01c5-4b54-bfac-e108b6ceb32a",
                    "level": 2,
                    "sort": 5,
                    "type": 2,
                    "path": "/asd",
                    "title": "角色管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "014bf6d7-cff3-4b61-aa3b-f59cc0dd3235",
                    "parentId": "43767678-01c5-4b54-bfac-e108b6ceb32a",
                    "level": 2,
                    "sort": 6,
                    "type": 2,
                    "path": "/asd",
                    "title": "策略管理",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "d27462d2-b316-4983-9066-f283fcf17e46",
                    "parentId": "43767678-01c5-4b54-bfac-e108b6ceb32a",
                    "level": 2,
                    "sort": 7,
                    "type": 2,
                    "path": "/asd",
                    "title": "系统管理",
                    "icon": null,
                    "children": null
                }
            ]
        },
        {
            "id": "da3af6df-cd00-4746-b788-bd2dfeab716f",
            "parentId": "0",
            "level": 1,
            "sort": 5,
            "type": 1,
            "path": null,
            "title": "我的管理",
            "icon": "home",
            "children": [
                {
                    "id": "922666ba-1ed1-4b6b-9043-30aeead1eebe",
                    "parentId": "da3af6df-cd00-4746-b788-bd2dfeab716f",
                    "level": 2,
                    "sort": 1,
                    "type": 2,
                    "path": "/asd",
                    "title": "我的授权",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "23a8778f-9424-449a-a68e-54b12e4c30f3",
                    "parentId": "da3af6df-cd00-4746-b788-bd2dfeab716f",
                    "level": 2,
                    "sort": 2,
                    "type": 2,
                    "path": "/asd",
                    "title": "我的卡密",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "cd256048-3f8e-4fb7-ada3-55d538ce7611",
                    "parentId": "da3af6df-cd00-4746-b788-bd2dfeab716f",
                    "level": 2,
                    "sort": 3,
                    "type": 2,
                    "path": "/asd",
                    "title": "我的余额",
                    "icon": null,
                    "children": null
                }
            ]
        }
    ],
    "timestamp": 1646991442708
}

代理结果展示

{
    "code": 200,
    "success": true,
    "msg": "获取成功",
    "sign": "",
    "result": [
        {
            "id": "da3af6df-cd00-4746-b788-bd2dfeab716f",
            "parentId": "0",
            "level": 1,
            "sort": 5,
            "type": 1,
            "path": null,
            "title": "我的管理",
            "icon": "home",
            "children": [
                {
                    "id": "922666ba-1ed1-4b6b-9043-30aeead1eebe",
                    "parentId": "da3af6df-cd00-4746-b788-bd2dfeab716f",
                    "level": 2,
                    "sort": 1,
                    "type": 2,
                    "path": "/asd",
                    "title": "我的授权",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "23a8778f-9424-449a-a68e-54b12e4c30f3",
                    "parentId": "da3af6df-cd00-4746-b788-bd2dfeab716f",
                    "level": 2,
                    "sort": 2,
                    "type": 2,
                    "path": "/asd",
                    "title": "我的卡密",
                    "icon": null,
                    "children": null
                },
                {
                    "id": "cd256048-3f8e-4fb7-ada3-55d538ce7611",
                    "parentId": "da3af6df-cd00-4746-b788-bd2dfeab716f",
                    "level": 2,
                    "sort": 3,
                    "type": 2,
                    "path": "/asd",
                    "title": "我的余额",
                    "icon": null,
                    "children": null
                }
            ]
        }
    ],
    "timestamp": 1646991115335
}

数据库

menu表结构

java返回数据库中层级结构数据的纯算法写法,以动态菜单为例_2d

menu表数据

java返回数据库中层级结构数据的纯算法写法,以动态菜单为例_2d_02

role表结构,仅为演示

java返回数据库中层级结构数据的纯算法写法,以动态菜单为例_2d_03

role表数据,仅为演示

超级管理员分配了全部菜单权限

代理角色只分配了4个菜单权限 ——我的管理 ————我的授权 ————我的卡密 ————我的余额

java返回数据库中层级结构数据的纯算法写法,以动态菜单为例_List_04

实体类

menu

package cn.myauthx.api.main.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author DaenMax
 * @since 2022-01-06
 */
@Data
@Accessors(chain = true)
@TableName("ma_menu")
public class Menu extends Model {

    private static final long serialVersionUID = 1L;

    private String id;

    /**
     * 父ID,根则空
     */
    private String parentId;


    /**
     * 层级,从1开始
     */
    private Integer level;

    /**
     * 排序,越小越大,从1开始
     */
    private Integer sort;

    /**
     * 1=目录,2=菜单
     */
    private Integer type;

    private String path;
    private String title;
    private String icon;

    @TableField(exist = false)
    private List<Menu> children;

}

role

package cn.myauthx.api.main.entity;

import cn.myauthx.api.base.po.baseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * <p>
 *
 * </p>
 *
 * @author DaenMax
 * @since 2022-03-09
 */
@Getter
@Setter
@Accessors(chain = true)
@TableName("ma_role")
public class Role extends baseEntity {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 角色名
     */
    private String name;
    /**
     * 0=超级管理员
     */
    private Integer fromSoftId;

    /**
     * 只存menu的id,json数组
     */
    private String meunIds;

    /**
     * 折扣,单位百分%
     */
    private Integer discount;


}

算法

/**
     * 获取权限菜单,算法版
     *
     * @param admin
     * @return
     */
    @Override
    public Result getMenuList(Admin admin) {
        List<Menu> tmpMenuList = new ArrayList<>();
        Role role = roleMapper.selectById(admin.getRole());
        if (CheckUtils.isObjectEmpty(role.getMeunIds())) {
            return Result.error("没有任何菜单", tmpMenuList);
        }
        JSONArray jsonArray = JSONArray.parseArray(role.getMeunIds());
        if (jsonArray.size() == 0) {
            return Result.error("没有任何菜单", tmpMenuList);
        }
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.in(Menu::getId, jsonArray);
        menuLambdaQueryWrapper.orderBy(true, true, Menu::getLevel);
        List<Menu> menuList = menuMapper.selectList(menuLambdaQueryWrapper);
        if (menuList.size() == 0) {
            return Result.error("没有任何菜单", tmpMenuList);
        }
        //最深的层次数
        Integer maxLevel = menuList.get(menuList.size() - 1).getLevel();
        //从最里层开始循环
        for (Integer i = maxLevel; i > 0; i--) {
            List<Menu> menuListByLevel = getMenuListByLevel(menuList, i);
            if (i.equals(maxLevel)) {
                tmpMenuList = menuListByLevel;
            } else {
                //外循环父节点
                for (Menu menu : menuListByLevel) {
                    List<Menu> children = new ArrayList<>();
                    //内循环子节点
                    for (Menu tmpMenu : tmpMenuList) {
                        if (menu.getId().equals(tmpMenu.getParentId())) {
                            children.add(tmpMenu);
                        }
                    }
                    menu.setChildren(children);
                }
                tmpMenuList = menuListByLevel;
            }
        }
        return Result.ok("获取成功", tmpMenuList);
    }

    /**
     * 取指定level(层次)的数据
     *
     * @param list
     * @param level
     * @return
     */
    public List<Menu> getMenuListByLevel(List<Menu> list, Integer level) {
        List<Menu> newMenuList = new ArrayList<>();
        for (Menu menu : list) {
            if (level.equals(menu.getLevel())) {
                newMenuList.add(menu);
            }
        }
        sortMenuList(newMenuList);
        return newMenuList;
    }

    /**
     * 按照sort字段排序
     *
     * @param list
     */
    public void sortMenuList(List<Menu> list) {
        list.sort((menu1, menu2) -> {
            Integer sort1 = menu1.getSort();
            Integer sort2 = menu2.getSort();
            return sort1.compareTo(sort2);
        });
    }