菜单子节点的写法

菜单子节点的写法

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"
]
相关推荐
程序员-小李7 分钟前
餐厅下单助手系统(Java+MySQL)
java·开发语言·mysql
开心工作室_kaic11 分钟前
springboot496基于java手机销售网站设计和实现(论文+源码)_kaic
java·开发语言·智能手机
像少年啦飞驰点、13 分钟前
SpringBoot + HttpSession 自定义生成sessionId
java·开发语言
珊珊来吃17 分钟前
EXCEL中给某一列数据加上双引号
java·前端·excel
我曾经是个程序员25 分钟前
使用C#生成一张1G大小的空白图片
java·算法·c#
向阳121829 分钟前
mybatis SqlSessionFactory
java·mybatis
mask哥29 分钟前
算法:LeetCode470_用Rand7()实现Rand10()_java实现
java·开发语言
Suwg20934 分钟前
《手写Mybatis渐进式源码实践》实践笔记(第七章 SQL执行器的创建和使用)
java·数据库·笔记·后端·sql·mybatis·模板方法模式
丁总学Java37 分钟前
优化 invite_codes 表的 SQL 创建语句
java·数据库·sql
抓哇FullStack-Junior40 分钟前
设计模式——适配器模式
java·设计模式·适配器模式