最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn)

 更新時(shí)間:2022年04月19日 08:53:09   作者:華大哥  
本文主要介紹了SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

公司有個(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的使用詳解

    本文主要介紹了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

    這篇文章主要介紹了Hibernate Validator實(shí)現(xiàn)更簡潔的參數(shù)校驗(yàn)及一個(gè)util,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • 使用 Java 開發(fā) Gradle 插件的步驟

    使用 Java 開發(fā) Gradle 插件的步驟

    這篇文章主要介紹了使用 Java 開發(fā) Gradle 插件的步驟,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • eclipse啟動出現(xiàn)“failed to load the jni shared library”問題解決

    eclipse啟動出現(xiàn)“failed to load the jni shared library”問題解決

    這篇文章主要介紹了eclipse啟動出現(xiàn)“failed to load the jni shared library”問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • spring注解在自定義jar包中無法被掃描的解決方案

    spring注解在自定義jar包中無法被掃描的解決方案

    這篇文章主要介紹了spring注解在自定義jar包中無法被掃描的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java 3D入門之基本圖形功能 附源碼

    Java 3D入門之基本圖形功能 附源碼

    Java3D API是Sun定義的用于實(shí)現(xiàn)3D顯示的接口。3D技術(shù)是底層的顯示技術(shù),Java3D提供了基于Java的上層接口。Java3D把OpenGL和DirectX這些底層技術(shù)包裝在Java接口中。這種全新的設(shè)計(jì)使3D技術(shù)變得不再繁瑣且可以加入到J2SE、J2EE的整套架構(gòu),故保證了Java3D技術(shù)強(qiáng)大的擴(kuò)展性
    2021-10-10
  • Java?RabbitMQ的持久化和發(fā)布確認(rèn)詳解

    Java?RabbitMQ的持久化和發(fā)布確認(rèn)詳解

    這篇文章主要為大家詳細(xì)介紹了RabbitMQ的持久化和發(fā)布確認(rèn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • mybatis-plus @select動態(tài)查詢方式

    mybatis-plus @select動態(tài)查詢方式

    這篇文章主要介紹了mybatis-plus @select動態(tài)查詢方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • JavaWeb實(shí)現(xiàn)文件的上傳與下載

    JavaWeb實(shí)現(xiàn)文件的上傳與下載

    這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)文件的上傳與下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Spring Boot 單元測試JUnit的實(shí)踐

    Spring Boot 單元測試JUnit的實(shí)踐

    JUnit是一款優(yōu)秀的開源Java單元測試框架,也是目前使用率最高最流行的測試框架,這篇文章主要介紹了Spring Boot 單元測試JUnit的實(shí)踐,感興趣的小伙伴們可以參考一下
    2018-11-11

最新評論

磴口县| 仙桃市| 静海县| 平顺县| 浦北县| 塔河县| 镇巴县| 万载县| 浦东新区| 甘南县| 丹凤县| 宣威市| 遂昌县| 贡觉县| 大悟县| 科尔| 兴化市| 石屏县| 金坛市| 凤山县| 平凉市| 固镇县| 汨罗市| 张掖市| 邵武市| 阳春市| 瓮安县| 尼勒克县| 开化县| 潢川县| 年辖:市辖区| 延长县| 黄龙县| 天水市| 清原| 台南市| 巨鹿县| 和平区| 岫岩| 法库县| 苏尼特右旗|