菜单子节点的写法
- 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层代码
@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.测试结果
[
{
"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层代码
@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.测试结果
[
"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"
]