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>