最近在做一个树形结构数据的处理,类似于俄罗斯套娃那种无限层数的那种树形结构,查询出所有数据并按层级进行展示
于是做了一个小demo,数据库大概如下:
处理完的数据如下:
[
{
"id": 1,
"name": "零食",
"desc": "最初级",
"pid": 0,
"nodeSign": 1,
"childrenList": [
{
"id": 2,
"name": "水果",
"desc": "第二级",
"pid": 1,
"nodeSign": 1,
"childrenList": [
{
"id": 5,
"name": "苹果",
"desc": "三级",
"pid": 2,
"nodeSign": 1,
"childrenList": [
{
"id": 11,
"name": "红富士苹果",
"desc": "四级",
"pid": 5,
"nodeSign": 0,
"childrenList": []
}
]
},
{
"id": 6,
"name": "香蕉",
"desc": "三级",
"pid": 2,
"nodeSign": 1,
"childrenList": [
{
"id": 12,
"name": "芝麻香蕉",
"desc": "四级",
"pid": 6,
"nodeSign": 0,
"childrenList": []
}
]
}
]
},
{
"id": 3,
"name": "方便面",
"desc": "第二级",
"pid": 1,
"nodeSign": 1,
"childrenList": [
{
"id": 7,
"name": "泡面",
"desc": "三级",
"pid": 3,
"nodeSign": 1,
"childrenList": [
{
"id": 13,
"name": "红烧牛肉面",
"desc": "四级",
"pid": 7,
"nodeSign": 0,
"childrenList": []
}
]
},
{
"id": 8,
"name": "干脆面",
"desc": "三级",
"pid": 3,
"nodeSign": 1,
"childrenList": [
{
"id": 14,
"name": "小浣熊干脆面",
"desc": "四级",
"pid": 8,
"nodeSign": 0,
"childrenList": []
}
]
}
]
},
{
"id": 4,
"name": "牛奶",
"desc": "第二级",
"pid": 1,
"nodeSign": 1,
"childrenList": [
{
"id": 9,
"name": "纯牛奶",
"desc": "三级",
"pid": 4,
"nodeSign": 1,
"childrenList": [
{
"id": 15,
"name": "纯甄纯牛奶",
"desc": "四级",
"pid": 9,
"nodeSign": 0,
"childrenList": []
}
]
},
{
"id": 10,
"name": "酸牛奶",
"desc": "三级",
"pid": 4,
"nodeSign": 1,
"childrenList": [
{
"id": 16,
"name": "安慕希酸奶",
"desc": "四级",
"pid": 10,
"nodeSign": 0,
"childrenList": []
}
]
}
]
}
]
},
{
"id": 17,
"name": "玩乐",
"desc": "最初级",
"pid": 0,
"nodeSign": 1,
"childrenList": [
{
"id": 18,
"name": "ak47",
"desc": "第二级",
"pid": 17,
"nodeSign": 0,
"childrenList": []
}
]
}
]
ok,下面是实现过程:
实体类:
@Data
public class User {
private Integer id;
private String name;
private String desc;
private Integer pid;
private Integer nodeSign;
private List<User> childrenList;
}
1.controller层:
@GetMapping("/allUser")
public List<User> findAllUser(){
List<User> userList = userService.findAllUser();
return userList;
}
2.实现层:
@Override
public List<User> findAllUser() {
List<User> userList = userMapper.findAllUser();
List<User> mainList = Lists.newArrayList();
mainList =
userList.stream().filter(e -> 0 == e.getPid()).collect(Collectors.toList());
for (User user : mainList) {
List<User> childrenList = this.childrenList(user,
userList);
user.setChildrenList(childrenList);
}
return mainList;
}
private List<User> childrenList(User user, List<User> userList) {
List<User> childrenList = Lists.newLinkedList();
if (null != userList && userList.size() > 0) {
childrenList =
userList.stream().filter(e -> user.getId().equals(e.getPid())).collect(Collectors.toList());
for (int i = 0; i < childrenList.size(); i++) {
User nodeUser = childrenList.get(i);
List<User> list =
userList.stream().filter(e -> nodeUser.getId().equals(e.getPid())).collect(Collectors.toList());
if (list.size() > 0) {
this.childrenList(nodeUser, userList);
}
nodeUser.setChildrenList(list);
}
}
return childrenList;
}
3.dao层:
List<User> findAllUser();
4.mapper.xml:
<mapper namespace="com.zx.zxtest.mapper.UserMapper">
<select id="findAllUser" resultType="com.zx.zxtest.entity.User">
select * from user
</select>
</mapper>
先查询出所有数据,然后通过stream流处理集合数据,实现树形数据的处理,处理速度也是非常的快!!!
标签:name,递归,查询处理,pid,childrenList,树形,nodeSign,id,desc From: https://blog.51cto.com/u_11585227/7597581