SpringBoot、mybatis返回树结构的数据实现

作者:华大哥 时间:2022-05-12 18:56:08 

公司有个业务需要查出所有的用户权限分类,并将最后一层类别所包含的权限查出来。

SpringBoot、mybatis返回树结构的数据实现

数据库说明,有一个parent_id 字段是最好的:、

SpringBoot、mybatis返回树结构的数据实现

parent_id的值就是上级的id,一般的话,最顶级的parent_id是设置为0

先看看表结构:

SpringBoot、mybatis返回树结构的数据实现

 下面不说废话,直接上代码:

定义的vo类:

@ApiModelProperty("id")
   private Long id;

@ApiModelProperty("父ID")
   private Long parentId;

@ApiModelProperty("名称")
   private String name;

@ApiModelProperty("子节点")
   private List<UserVo> children;

 获取列表

List<UserVo>  userList userService.findUsersAndChildrenList(User);
   List<UserVo> users = new ArrayList<>();
       for (UserVo r : userList) {
           UserVo user = new UserVo();
           user.setId(r.getId());
           user.setParentId(r.getParentId());
           user.setName(r.getName());
           List<UserVo>  children = this.getChildrenList(r.getId(), status);
           user.setChildren(children);
           users.add(user);
    }
public List<UserVo> getChildrenList(Long cid){
       List<UserVo> users=  userService.findUserChildrenByParentId(cid);
       List<UserVo> userList= new ArrayList<>();
       if(users){
           for (UserVo u : users) {
               UserVo user = new UserVo();
               user.setId(u.getId());
               user.setName(u.getName());
               user.setParentId(u.getParentId());
               List<UserVo >  children = this.getChildrenList(u.getId());
               user.setChildren(children);
               userList.add(user);
           }
       }
       return  userList;
   }

 mybatis查询:

<select id="findUserChildrenList" resultMap="BaseResultMap">
       SELECT *
       FROM user
       WHERE parent_id=#{id}
</select>

最终的数据结构:

{
   "message":'获取成功',
   "data":{
       "num":1,
       "pageSize":20,
       "total":1,
       "list":[
           {
               "id":6,
               "name":"测试",
               "parent_id":1,
               "children":[
                   {
                       "id":9,
"name":"测试1",
"parent_id":6,
                       "children":[
                           {
                               "id":20,
"name":"测试2",
"parent_id":9,
                               "children":[
                                   {
                                       "id":21,
"name":"测试3",
"parent_id":20,
                                   },
                                   {
                                       "id":22,
"name":"测试4",
"parent_id":20,
                                   },
                                   {
                                        "id":23,
"name":"测试5",
"parent_id":20,
                                   }
                               ],
                           }
                       ],
                   },
               ],
           }
       ]
   },
   "code":200
}

 如果要查某个节点的所有父节点:

 mybatis查询改为 :

<select id="findUserParentListById" resultMap="BaseResultMap">
       SELECT *
       FROM user
       WHERE id=#{id}
</select>
public List<UserVo> getParentList(Long cid){
       List<UserVo> users=  userService.findUserParentListById(cid);
       List<UserVo> userList= new ArrayList<>();
       if(users){
           for (UserVo u : users) {
               UserVo user = new UserVo();
               user.setId(u.getId());
               user.setName(u.getName());
               user.setParentId(u.getParentId());
               List<UserVo >  children = this.getParentList(u.getParentId());
               user.setChildren(children);
               userList.add(user);
           }
       }
       return  userList;
   }

来源:https://blog.csdn.net/lchmyhua88/article/details/124228470

标签:SpringBoot,mybatis,返回树结构
0
投稿

猜你喜欢

  • C++ Boost MPI接口详细讲解

    2023-11-02 13:35:36
  • java实现日期拆分的方法

    2023-06-19 00:28:59
  • java线程池ThreadPoolExecutor类使用小结

    2021-09-10 16:22:05
  • Spring bean为什么需要依赖注入

    2022-01-24 11:07:21
  • Java中的ThreadLocal详解

    2023-01-02 16:02:21
  • java常用工具类 Random随机数、MD5加密工具类

    2023-02-14 17:55:08
  • Android Flutter使用本地数据库编写备忘录应用

    2023-09-15 17:24:09
  • Java多线程和并发基础面试题(问答形式)

    2022-03-29 17:39:42
  • Kotlin基础教程之数据类型

    2023-12-06 15:23:22
  • java使用字符画一个海绵宝宝

    2023-09-08 09:45:19
  • C++找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)

    2023-06-22 07:32:31
  • Java 实现repalceAll只替换第二个匹配到的字符串

    2021-06-12 11:56:20
  • C#根据http和ftp图片地址获取对应图片

    2023-06-06 02:00:23
  • springcloud Zuul动态路由的实现

    2021-10-07 06:15:40
  • Ajax实现省市区三级联动

    2023-01-14 05:09:58
  • java实现连连看游戏课程设计

    2023-10-30 13:18:37
  • 简单谈谈Java 中的线程的几种状态

    2023-05-10 13:29:45
  • Java String源码分析并介绍Sting 为什么不可变

    2021-09-23 06:10:42
  • 教你使用Java获取当前时间戳的详细代码

    2021-09-19 04:41:02
  • SpringBoot项目在IntelliJ IDEA中如何实现热部署

    2023-10-29 13:30:22
  • asp之家 软件编程 m.aspxhome.com