SpringBoot、mybatis返回樹(shù)結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn)
公司有個(gè)業(yè)務(wù)需要查出所有的用戶(hù)權(quán)限分類(lèi),并將最后一層類(lèi)別所包含的權(quán)限查出來(lái)。
數(shù)據(jù)庫(kù)說(shuō)明,有一個(gè)parent_id 字段是最好的:、
parent_id的值就是上級(jí)的id,一般的話,最頂級(jí)的parent_id是設(shè)置為0。
先看看表結(jié)構(gòu):
下面不說(shuō)廢話,直接上代碼:
定義的vo類(lèi):
@ApiModelProperty("id") private Long id; @ApiModelProperty("父ID") private Long parentId; @ApiModelProperty("名稱(chēng)") private String name; @ApiModelProperty("子節(jié)點(diǎn)") 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查詢(xún):
<select id="findUserChildrenList" resultMap="BaseResultMap"> SELECT * FROM user WHERE parent_id=#{id} </select>
最終的數(shù)據(jù)結(jié)構(gòu):
{ "message":'獲取成功', "data":{ "num":1, "pageSize":20, "total":1, "list":[ { "id":6, "name":"測(cè)試", "parent_id":1, "children":[ { "id":9, "name":"測(cè)試1", "parent_id":6, "children":[ { "id":20, "name":"測(cè)試2", "parent_id":9, "children":[ { "id":21, "name":"測(cè)試3", "parent_id":20, }, { "id":22, "name":"測(cè)試4", "parent_id":20, }, { "id":23, "name":"測(cè)試5", "parent_id":20, } ], } ], }, ], } ] }, "code":200 }
如果要查某個(gè)節(jié)點(diǎn)的所有父節(jié)點(diǎn):
mybatis查詢(xún)改為 :
<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; }
到此這篇關(guān)于SpringBoot、mybatis返回樹(shù)結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot、mybatis返回樹(shù)結(jié)構(gòu) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合MybatisPlus實(shí)現(xiàn)增刪改查功能
- SpringBoot整合MyBatis-Plus樂(lè)觀鎖不生效的問(wèn)題及解決方法
- springboot整合mybatis的超詳細(xì)過(guò)程(配置模式+注解模式)
- SpringBoot中的Mybatis依賴(lài)問(wèn)題
- SpringBoot整合Mybatis-plus的具體使用
- Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢(xún)功能
- 基于SpringBoot使用MyBatis插件的問(wèn)題
- springboot整合mybatisplus的方法詳解
- springboot整合mybatis流程詳解
相關(guān)文章
Java語(yǔ)法之 Java 的多態(tài)、抽象類(lèi)和接口
上節(jié)介紹了 Java 基礎(chǔ)語(yǔ)法之解析 Java 的包和繼承,如果這類(lèi)知識(shí)有點(diǎn)疑惑的小伙伴,可以去 Java 的包和繼承 這章看看,或許可以幫你解決一些疑惑喲!今天這篇文章我們將講解的是 Java 的多態(tài)、抽象類(lèi)和接口,感興趣的小伙伴可以參考下面文章的具體內(nèi)容2021-09-09java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作
這篇文章主要介紹了java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08idea中MavenWeb項(xiàng)目不能創(chuàng)建Servlet的解決方案
這篇文章主要介紹了idea中MavenWeb項(xiàng)目不能創(chuàng)建Servlet的解決方案,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02