java返回数据库中层级结构数据的treeListMap写法,以动态菜单为例
说明
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写法,以动态菜单为例的更多相关文章
- Eclipse中java向数据库中添加数据,更新数据,删除数据
前面详细写过如何连接数据库的具体操作,下面介绍向数据库中添加数据. 注意事项:如果参考下面代码,需要 改包名,数据库名,数据库账号,密码,和数据表(数据表里面的信息) package com.ning ...
- java和数据库中日期类型的常见用法
(1)java中日期类型:Date.Timestamp(2)数据库中:Date.Timestamp(3)字符串和Date之间的格式化转换: SimpleDateFormat类方法: format ...
- java和数据库中所有的锁都在这了
1.java中的锁 1.1 锁的种类 公平锁/非公平锁 可重入锁/不可重入 独享锁/共享锁 读写锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 1.2 锁详细介绍 1.2.1 公平锁,非公平锁 公平锁 ...
- Java删除数据库中的数据
1:删除数据库中数据表中的数据同样也是一个非常用的技术,使用executeUpdate()方法执行用来做删除SQL的语句可以删除数据库表中的数据 2:本案例使用Statement接口中的execute ...
- java更改数据库中的数据
不废话,上代码 package com.ningmeng; import java.sql.*; /** * 1:更改数据库中的数据 * @author biexiansheng * */ publi ...
- 时间、金钱在java、数据库中的变量类型之总结
在编写程序时,总是有些变量的类型搞不很明白,现将目前涉及到的变量总结一下: 1.“时间”类型 (1).在数据库中的变量类型是:DateTime 比如: operateTime DATETIME,//数 ...
- java 将数据库中的每一条数据取出放入数组或者List中
1.如何将数据库中数据按照行(即一整条数据)取出来,存入到数组当中? public static String str = null; // 将StringBuffer转化成字符串 public st ...
- 利用JAVA想数据库中提交数据
1.用户信息提交界面 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- Java读取数据库中的xml格式内容,解析后修改属性节点内容并写回数据库
直接附代码: 1.测试用的xml内容 <mxGraphModel> <root> <mxCell id="-1" /> <mxCell i ...
- java返回参数中几种常见的方法
1.有参数有返回值 public class text_1 { 1)创建add方法 public int add(int i, int j) { int res = i + j; ...
随机推荐
- python excel 读取:如何读取符合多个条件的记录【出差、外出、调休、年假】
if 语句结合or 实现:读取所有出差.外出.调休.年假的记录 if '出差' in str(c_cell) or '外出' in str(c_cell) or'调休' in str(c_cell) ...
- QT5.14.1+Win7 64+Oracle11gR2 Qt连接数据库
原文链接 1.QT5.14下OCI驱动编译完整步骤 1.安装qt的时候手动选择安装源码资源(默认不安装Source的) 2.进入QT安装目录下E:\Qt5.14\5.14.0\Src\qtbase\s ...
- 赞美Syscall
知道程序员在编写程序时直接和硬件打交道是一件非常麻烦的事,同时不受保障的程序直接运行在硬件上如果出错可能造成计算机宕机,于是可以编写一个应用程序驻留在计算机上管理硬件资源为上层应用提供系统调用(sys ...
- kubernetes failed to create kubelet: misconfiguration: kubelet cgroup driver: "cgroupfs" is different from docker cgroup driver: "systemd"
错误原因 kubernetes 的文件驱动与 docker 不一致,导致镜像无法启动. docker info 可以看到驱动方式 Cgroup Driver: systemd. 解决方案 统一资源管理 ...
- vue学习一(指令1.v-text,v-html,插值表达式{{msg}})
一.1.v-text,v-html,插值表达式{{msg}} 注:v-text解决差值表达式闪烁问题,因为他是属性不是差值表达式 1.1.v-text: 是没有闪烁问题的,会覆盖标签的元素中原本的内容 ...
- SpringBoot+微信支付-JSAPI{微信支付回调}
引入微信支付SDK Maven: com.github.wechatpay-apiv3:wechatpay-java-core:0.2.12 Maven: com.github.wechatpay-a ...
- Windows 下 Toad 如何使用 Oracle instantclient 32位客户端
Toad需要32位的Oracle客户端,通过如下方法,可以使用 Oracle instantclient. 1.安装 Toad, 2.下载 Oracle instantclient 32位版,解压到指 ...
- 关于SIFT,GIFT在旋转不变性上的对比实验
目录 关于SIFT,GIFT在旋转不变性上的对比实验 回顾 准确率测试 总结 核心代码 关于SIFT,GIFT在旋转不变性上的对比实验 这篇文章不讨论SIFT,GIFT的实现原理,只从最终匹配结果的准 ...
- K8s Ingress, 你这个老6
本文是有态度马甲的第185篇原创. 本文记录了k8s中核心对象Ingress的产生背景和实现机制. 我们都知道k8s Service是一种将Pods通过网络暴露出来的抽象,每个服务定义了一组有关Pod ...
- 【Java】常用类
一.String类 java.lang.String类的使用 注意:String可以String s = "";,是因为String类型在后面自动补充了'\0' char初始化不能 ...