首頁 > 軟體

SpringBoot、mybatis返回樹結構的資料實現

2022-04-19 10:01:19

公司有個業務需要查出所有的使用者許可權分類,並將最後一層類別所包含的許可權查出來。

資料庫說明,有一個parent_id 欄位是最好的:、

parent_id的值就是上級的id,一般的話,最頂級的parent_id是設定為0

先看看錶結構:

 下面不說廢話,直接上程式碼:

定義的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;
    }

到此這篇關於SpringBoot、mybatis返回樹結構的資料實現的文章就介紹到這了,更多相關SpringBoot、mybatis返回樹結構 內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com