SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn)
公司有個(gè)業(yè)務(wù)需要查出所有的用戶權(quán)限分類,并將最后一層類別所包含的權(quán)限查出來。

數(shù)據(jù)庫說明,有一個(gè)parent_id 字段是最好的:、

parent_id的值就是上級的id,一般的話,最頂級的parent_id是設(shè)置為0。
先看看表結(jié)構(gòu):

下面不說廢話,直接上代碼:
定義的vo類:
@ApiModelProperty("id")
private Long id;
@ApiModelProperty("父ID")
private Long parentId;
@ApiModelProperty("名稱")
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查詢:
<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":"測試",
"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
}如果要查某個(gè)節(jié)點(diǎn)的所有父節(jié)點(diǎn):
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;
}到此這篇關(guān)于SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot、mybatis返回樹結(jié)構(gòu) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解
本文主要介紹了Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Hibernate Validator實(shí)現(xiàn)更簡潔的參數(shù)校驗(yàn)及一個(gè)util
這篇文章主要介紹了Hibernate Validator實(shí)現(xiàn)更簡潔的參數(shù)校驗(yàn)及一個(gè)util,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
eclipse啟動(dòng)出現(xiàn)“failed to load the jni shared library”問題解決
這篇文章主要介紹了eclipse啟動(dòng)出現(xiàn)“failed to load the jni shared library”問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Java?RabbitMQ的持久化和發(fā)布確認(rèn)詳解
這篇文章主要為大家詳細(xì)介紹了RabbitMQ的持久化和發(fā)布確認(rèn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
mybatis-plus @select動(dòng)態(tài)查詢方式
這篇文章主要介紹了mybatis-plus @select動(dòng)態(tài)查詢方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

