菜单子节点的写法

菜单子节点的写法

1.测试数据

1.表结构SQL

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

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.查询表数据

sql 复制代码
select * from test;

2.实现代码

1.pojo代码

java 复制代码
@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层代码

java 复制代码
    @Override
    public 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.测试结果

json 复制代码
[
    {
        "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代码

java 复制代码
@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层代码

java 复制代码
 @Override
 public 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.测试结果

json 复制代码
[
    "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"
]
相关推荐
我命由我123452 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
武子康4 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
YuTaoShao6 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
源码_V_saaskw7 小时前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
超浪的晨7 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
双力臂4047 小时前
Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
java·spring boot·后端·单元测试
Edingbrugh.南空8 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
QQ_4376643148 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
永卿0018 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式
誰能久伴不乏9 小时前
Linux如何执行系统调用及高效执行系统调用:深入浅出的解析
java·服务器·前端