当前位置: 首页 > news >正文

菜单子节点的写法

菜单子节点的写法

  • 1.测试数据
  • 2.实现代码
  • 3.获取父ID层级

1.测试数据

1.表结构SQL

CREATE TABLE `test` (`id` int DEFAULT NULL,`u_id` int DEFAULT NULL,`p_u_id` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

2.数据SQL

INSERT INTO test (id,u_id,p_u_id) VALUES(1,1,0),(2,2,0),(3,3,0),(4,11,1),(5,12,1),(6,21,2),(7,22,2),(8,211,21),(9,221,22),(10,222,22),(11,223,22),(12,2231,223),(13,2232,223),(14,0,-1);

3.查询表数据

select * from test;

在这里插入图片描述

2.实现代码

1.pojo代码

@TableName("test")
@Data
public class Test implements Serializable {private Integer id;private Integer uId;private Integer pUId;@TableField(exist = false)private List<Test> testChains;@TableField(exist =false)private String treeId;}

2.service层代码

    @Overridepublic List<String> treeIds() {List<Test> list = this.list();return list.stream().filter(e -> e.getPUId() == -1).peek((tId) -> {tId.setTestChains(getChildren(tId, list));}).toList();}private List<Test> getChildren(Test test, List<Test> all) {return all.stream().filter(x->x.getPUId() != null).filter(categoryEntity -> {return categoryEntity.getPUId().equals(test.getUId());}).peek(treeId -> {treeId.setTestChains(getChildren(treeId, all));}).collect(Collectors.toList());}
}

3.测试结果

[{"id": 14,"testChains": [{"id": 1,"testChains": [{"id": 4,"testChains": [],"treeId": null,"puid": 1,"uid": 11},{"id": 5,"testChains": [],"treeId": null,"puid": 1,"uid": 12}],"treeId": null,"puid": 0,"uid": 1},{"id": 2,"testChains": [{"id": 6,"testChains": [{"id": 8,"testChains": [],"treeId": null,"puid": 21,"uid": 211}],"treeId": null,"puid": 2,"uid": 21},{"id": 7,"testChains": [{"id": 9,"testChains": [],"treeId": null,"puid": 22,"uid": 221},{"id": 10,"testChains": [],"treeId": null,"puid": 22,"uid": 222},{"id": 11,"testChains": [{"id": 12,"testChains": [],"treeId": null,"puid": 223,"uid": 2231},{"id": 13,"testChains": [],"treeId": null,"puid": 223,"uid": 2232}],"treeId": null,"puid": 22,"uid": 223}],"treeId": null,"puid": 2,"uid": 22}],"treeId": null,"puid": 0,"uid": 2},{"id": 3,"testChains": [],"treeId": null,"puid": 0,"uid": 3}],"treeId": null,"puid": -1,"uid": 0}
]

3.获取父ID层级

1.pojo代码

@TableName("test")
@Data
public class Test implements Serializable {private Integer id;private Integer uId;private Integer pUId;@TableField(exist = false)private List<Test> testChains;@TableField(exist =false)private String treeId;}

2.service层代码

 @Overridepublic List<String> treeIds() {List<Test> list = this.list();List<Test> collect = list.stream().peek((tId) -> {tId.setTreeId(tId.getUId().toString());tId.setTestChains(getChildren(tId, list));}).toList();return collect.stream().map(Test::getTreeId).toList();}private List<Test> getChildren(Test test, List<Test> all) {return all.stream().filter(x->x.getPUId() != null).filter(categoryEntity -> {return categoryEntity.getPUId().equals(test.getUId());}).peek(treeId -> {treeId.setTreeId((test.getTreeId() == null? test.getUId() : test.getTreeId())+"/"+treeId.getUId());treeId.setTestChains(getChildren(treeId, all));}).collect(Collectors.toList());}

3.测试结果

["0/1","0/2","0/3","0/1/11","0/1/12","0/2/21","0/2/22","0/2/21/211","0/2/22/221","0/2/22/222","0/2/22/223","0/2/22/223/2231","0/2/22/223/2232","0"
]
http://www.lryc.cn/news/186207.html

相关文章:

  • 系统架构设计:9 论软件系统架构评估及其应用
  • javaee SpringMVC中json的使用
  • 【系统架构】软件架构的演化和维护
  • 一盏茶的功夫帮你彻底搞懂JavaScript异步编程从回调地狱到async/await
  • 前后端分离计算机毕设项目之基于SpringBoot的无人智慧超市管理系统的设计与实现《内含源码+文档+部署教程》
  • 从0开始python学习-31.selenium 文本输入框、下拉选择框、文件上传、时间插件选择元素定位
  • MyCat-web安装文档:安装Zookeeper、安装Mycat-web
  • Ajax跨域访问,访问成功但一直走error不走success的的问题解决
  • 水星 Mercury MIPC251C-4 网络摄像头 ONVIF 与 PTZ 云台控制
  • Reactor 模式网络服务器【I/O多路复用】(C++实现)
  • 2019年[海淀区赛 第2题] 阶乘
  • CMM—软件企业走向世界的通行证
  • 基于FPGA的图像形态学腐蚀算法实现,包括tb测试文件和MATLAB辅助验证
  • 华为云云耀云服务器L实例评测|RabbitMQ的Docker版本安装 + 延迟插件安装 QQ邮箱和阿里云短信验证码的主题模式发送
  • 解决Linux安装AppImage文件chrome-sandbox出错问题
  • Axios、SASS学习笔记
  • 开发工作中常用到的免费API分享
  • 外汇天眼:三大方法提高容错率——成功投资者的秘密策略!
  • 设计模式-状态模式
  • 支持多种格式照片处理软件Lightroom Classic 2022 mac中文功能特点
  • UML简介
  • 【PostgreSQL内核学习(十七)—— (AutoAnalyze)】
  • C++中指向成员的指针运算符(.* 和 ->*)用法说明
  • ASUS华硕ZenBook灵耀X逍遥UXF3000E_UX363EA原装出厂预装Win11系统工厂模式安装包
  • 【数据结构】栈和队列-- OJ
  • 访问Apache Tomcat的管理页面
  • 企业组织内如何避免山头文化?
  • 【c#】线程Monitor.Wait和Monitor.Pulse使用
  • GitLab平台安装中经典安装语句含义解析
  • 湘潭大学 2023年下学期《C语言》作业0x03-循环1 XTU OJ 1094,1095,1096,1112,1113