首页 > 其他分享 >递归实现树形结构数据的查询处理

递归实现树形结构数据的查询处理

时间:2023-09-25 17:32:39浏览次数:39  
标签:name 递归 查询处理 pid childrenList 树形 nodeSign id desc

最近在做一个树形结构数据的处理,类似于俄罗斯套娃那种无限层数的那种树形结构,查询出所有数据并按层级进行展示

于是做了一个小demo,数据库大概如下:

递归实现树形结构数据的查询处理_List

处理完的数据如下:

[
    {
        "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

相关文章

  • 函数递归-汉诺塔问题
    1.问题描述:①有三根柱子X,Y,Z。X杆上有n只碟子②每次移动一块碟子,小的只能叠在大的上面③把所有碟子从X杆经Y杆全部移动到Z杆上.2.递归求解:①n<=1若只有一只碟子,直接X杆→Z杆;②n>1<1>把n-1只碟子按大小递减的次序从X杆经Z杆移动到Y杆;<2>将X杆上第n只碟子移到Z杆;<3>然后再将n-1......
  • 递归 (比较5个数的大小)
    #include<bits/stdc++.h>usingnamespacestd;intmax(inta[],intn){ intt; if(n==1){ t=a[1]; }else{ if(max(a,n-1)>a[n]){ t=max(a,n-1); }else{ t=n[a]; } } returnt;}intmain(intargc,char**argv){ inta[6]; for(inti=0;i......
  • 递归求最大值
    #include<bits/stdc++.h>usingnamespacestd;intt,n,a;intf(intn[],inta){ if(a==1){ t=n[1]; }else{ if(f(n,a-1)>n[a]){ t=f(n,a-1); }else{ t=n[a]; } } returnt;}intmain(){ intn[6],x; for(inti=0;i<5;i++){ cin>&g......
  • 递归求最大值
    #include<iostream>usingnamespacestd;intf(inta[],intn){ intt; if(n==1){ t=a[1]; }else{ if(f(a,n-1)>a[n]){ t=f(a,n-1); }else{ t=a[n]; } } returnt;}intmain(){ inta[6]; for(inti=1;i<=5;i++){ cin>>a[i]; ......
  • 树形DP
    什么是树形DP顾名思义,树形DP就是在某些题目中要求的树结构上使用DP的思想。树是有n个节点,n-1条边的无向图,且是无环的,联通的,又因为是无向图,所以两个节点间存在着相互的联通关系,有时需要加以判断当DP建立在依赖关系上时,就可以使用树形DP来解决问题。树形DP模板voiddfs(u,fa,ot......
  • 最大子树和(树形dp)
    题意题目链接:https://www.luogu.com.cn/problem/P1122给一棵树,树上的每个节点都有一个值,然后你可以剪掉一些节点,问最后你能得到的最大的和。(因为有些节点的值为负数。)思路典型树形dp。跑一遍dfs就行。从1开始搜,f[i]代表以i为根节点往下能得到的最大值,然后我们从......
  • 【Python】递归算法
    定义递归是一种解决问题的有效方法,在递归过程中,函数将自身作为子例程调用。思想函数调用函数本身,直到不能调用为止注意事项基本情况用于保证程序调用及时返回,不在继续递归,保证了程序可终止。递推关系,可将所有其他情况拆分到基本案例。​递推关系​:一个问题的结......
  • 递归
    1.全排列递归1#include<iostream>2usingnamespacestd;3intn,arr[10],ifuse[10]={0,0,0,0,0,0,0,0,0,0};4voidp()5{6for(intk=1;k<=n;k++)7{8cout<<arr[k]<<"";9}cout<......
  • PHP多层级菜单树形结构递归处理
    如题:一、数据库菜单数据表使用图片中id和parent_id两个参数来关联父子关系二、将数据库中的数据变成树状多层级解构```{ "id":1, "parentId":0, "treePath":"0", "name":"系统管理", "type":2, "path":"/system",......
  • 可变参数与递归
    可变参数上章说到方法重载,对于不同数量(输入的参数类型不一致会报错)的参数我们可以通过方法重载的方式来解决,如果参数数量具有很强的不确定性,我们不可能写n个方法来一个一个定义,因此可变参数诞生了在方法声明中在指定参数类型后加一个省略号(...)如:int...i但是要注意的是一个方......