菜单子节点的写法

菜单子节点的写法

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"
]
相关推荐
测开小菜鸟31 分钟前
使用python向钉钉群聊发送消息
java·python·钉钉
P.H. Infinity1 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天2 小时前
java的threadlocal为何内存泄漏
java
caridle2 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^2 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋32 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花2 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端2 小时前
第六章 7.0 LinkList
java·开发语言·网络
Wx-bishekaifayuan2 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源