说明

1.treeListMap写法,与纯算法版本比起来,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": [],
"ids": null
},
{
"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,
"ids": 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,
"ids": 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,
"ids": null
}
],
"ids": 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,
"ids": 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,
"ids": 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,
"ids": 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,
"ids": 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,
"ids": 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,
"ids": null
}
],
"ids": 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,
"ids": 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,
"ids": 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,
"ids": 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,
"ids": 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,
"ids": 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,
"ids": 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,
"ids": null
}
],
"ids": 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,
"ids": 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,
"ids": 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,
"ids": null
}
],
"ids": null
}
],
"timestamp": 1646988974738
}

代理结果展示

{
"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": [],
"ids": 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": [],
"ids": 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": [],
"ids": null
}
],
"ids": null
}
],
"timestamp": 1646990844854
}

数据库

menu表结构

menu表数据

role表结构,仅为演示

role表数据,仅为演示

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

代理角色只分配了4个菜单权限

——我的管理

————我的授权

————我的卡密

————我的余额

实体类

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; @TableField(exist = false)
private List<String> ids; }

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 getMenuListEx(Admin admin) {
Menu menu = new Menu();
menu.setParentId("0");
Role role = roleMapper.selectById(admin.getRole());
JSONArray jsonArray = JSONArray.parseArray(role.getMeunIds());
List<String> list = (List<String>) JSONArray.toJavaObject(jsonArray, List.class);
menu.setIds(list);
List<Menu> menus = menuMapper.treeList(menu);
return Result.ok(menus);
}

mapper

<resultMap id="treeListMap" type="cn.myauthx.api.main.entity.Menu" autoMapping="true">
<id property="id" column="ID"></id>
<collection property="children"
ofType="cn.myauthx.api.main.entity.Menu"
javaType="java.util.List"
column="{parentId=ID}"
select="treeList"></collection>
</resultMap>
<select id="treeList" parameterType="cn.myauthx.api.main.entity.Menu" resultMap="treeListMap">
select * from ma_menu
<where>
<if test="id!=null and id!=''">
and ID=#{id}
</if>
<if test="parentId!=null and parentId!=''">
and PARENT_ID=#{parentId}
</if>
<if test="ids !=null and ids.size()>0">
and id in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</where>
order by sort
</select>

java返回数据库中层级结构数据的treeListMap写法,以动态菜单为例的更多相关文章

  1. Eclipse中java向数据库中添加数据,更新数据,删除数据

    前面详细写过如何连接数据库的具体操作,下面介绍向数据库中添加数据. 注意事项:如果参考下面代码,需要 改包名,数据库名,数据库账号,密码,和数据表(数据表里面的信息) package com.ning ...

  2. java和数据库中日期类型的常见用法

    (1)java中日期类型:Date.Timestamp(2)数据库中:Date.Timestamp(3)字符串和Date之间的格式化转换:    SimpleDateFormat类方法: format ...

  3. java和数据库中所有的锁都在这了

    1.java中的锁 1.1 锁的种类 公平锁/非公平锁 可重入锁/不可重入 独享锁/共享锁 读写锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 1.2 锁详细介绍 1.2.1 公平锁,非公平锁 公平锁 ...

  4. Java删除数据库中的数据

    1:删除数据库中数据表中的数据同样也是一个非常用的技术,使用executeUpdate()方法执行用来做删除SQL的语句可以删除数据库表中的数据 2:本案例使用Statement接口中的execute ...

  5. java更改数据库中的数据

    不废话,上代码 package com.ningmeng; import java.sql.*; /** * 1:更改数据库中的数据 * @author biexiansheng * */ publi ...

  6. 时间、金钱在java、数据库中的变量类型之总结

    在编写程序时,总是有些变量的类型搞不很明白,现将目前涉及到的变量总结一下: 1.“时间”类型 (1).在数据库中的变量类型是:DateTime 比如: operateTime DATETIME,//数 ...

  7. java 将数据库中的每一条数据取出放入数组或者List中

    1.如何将数据库中数据按照行(即一整条数据)取出来,存入到数组当中? public static String str = null; // 将StringBuffer转化成字符串 public st ...

  8. 利用JAVA想数据库中提交数据

    1.用户信息提交界面 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...

  9. Java读取数据库中的xml格式内容,解析后修改属性节点内容并写回数据库

    直接附代码: 1.测试用的xml内容 <mxGraphModel> <root> <mxCell id="-1" /> <mxCell i ...

  10. java返回参数中几种常见的方法

    1.有参数有返回值 public class text_1 {    1)创建add方法 public int add(int i, int j) {        int res = i + j;  ...

随机推荐

  1. MAMP使用简单教程

    这个配置,没有域名访问,平时可以放些demo使用,如果需要域名访问请看MAMP PRO教程 启用服务 打开Launchpad中灰色的MAMP,进入界面后,点击Preferences,然后只需拿着鼠标点 ...

  2. Django实战项目-学习任务系统-发送短信通知

    接着上期代码内容,继续完善优化系统功能. 本次增加发送短信通知功能,学习任务系统发布的任务,为了更加及时通知到学生用户,再原有发送邮件通知基础上,再加上手机短信通知功能. 第一步:开通短信通知服务 目 ...

  3. 连接MySQL数据库出现时Authentication plugin 'caching_sha2_password' cannot be loaded的解决办法

    问题描述:用Navicat Premium或HeidiSQL连接MySQL数据库时会弹出下面的情况 解决方法: 1.运行命令行窗口,输入以下命令,输入密码后进入到mysql中,(最好将MySQL安装目 ...

  4. Ubuntu给Appimage创建快捷方式

    下载 AppImageLauncher 2.安装 3.选择要运行的Appimage 双击运行即可.他会在home目录下创建一个applications文件夹,并且帮你自动创建快捷方式.

  5. 基于.NetCore开发 StarBlog 番外篇 (2) 深入解析Markdig源码,优化ToC标题提取和文章目录树生成逻辑

    前言 虽然现在工作重心以AI为主了,不过相比起各种大模型的宏大叙事,我还是更喜欢自己构思功能.写代码,享受解决问题和发布上线的过程. 之前 StarBlog 系列更新的时候我也有提到,随着功能更新,会 ...

  6. 用于线程同步的Interlocked系列函数主要有哪些

    原子访问 通过Interlocked系列函数是 Windows API 提供的一组原子操作函数,用于在多线程环境中安全地操作共享变量.当我们执行这些Interlocked系列函数的时候 ,函数会对总线 ...

  7. 【Git】基本操作

    一.Git 基础 1.Git 介绍 Git 是目前世界上最先进的分布式版本控制系统. 版本控制系统: 设计师在设计的时候做了很多版本 经过了数天去问设计师每个版本都改了些啥,设计师此时可能就说不上来了 ...

  8. MySQL 默认字符集

    MySQL 默认的字符集是 latin1,通常跟我们的中文会打架,导致读写异常或乱码,而我们需要的字符集是 utf-8. MySQL 字符集的查看 mysql> show variables l ...

  9. vue & font-awesome

    vue & font-awesome // 使用npm安装依赖 npm install font-awesome@4.7.0 --save --verbose // 会在包管理文件(packa ...

  10. 二叉树专题学习(C++版) 基础的上机题

    前言: 由于二叉树这一章的题型比较多,涉及到的递归程序也较多,所以单开一个随笔来记录这个学习过程,希望对读者有帮助. 理论知识基础 在二叉树的选择题中,常常会涉及到对于最多或最少结点.最大或最小高度. ...