一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún)的方法

springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún)的方法

2022-03-08 00:56qzxl Java教程

本文主要介紹了springboot怎樣構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

因?yàn)轫?xiàng)目需要,頁(yè)面上需要樹(shù)形結(jié)構(gòu)的數(shù)據(jù)進(jìn)行展示(類(lèi)似下圖這樣),因此需要后端返回相應(yīng)格式的數(shù)據(jù)。

springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún)的方法

不說(shuō)廢話,直接開(kāi)干!!!

我這里用的是springboot+mybatis-plus+mysql,示例的接口是查詢(xún)一級(jí)權(quán)限以及二級(jí)權(quán)限、三級(jí)權(quán)限整個(gè)權(quán)限樹(shù)…

下面是導(dǎo)入的maven依賴(lài)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--數(shù)據(jù)庫(kù)連接-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.21</version>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--mybatis增強(qiáng)工具-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.0.6</version>
        </dependency>

下面是實(shí)體類(lèi)Permission

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@Data
public class Permission implements Serializable {
    @TableId
    private String permissionId;
    @NotNull(message = "權(quán)限名稱(chēng)不能為空")
    private String permissionName;
    /**
     * 權(quán)限標(biāo)識(shí)
     */
    @NotNull(message = "權(quán)限標(biāo)識(shí)不能為空")
    private String permissionCode;
    /**
     * 父菜單ID,如果是-1就表示是一級(jí)權(quán)限菜單。
     */
    @NotBlank(message = "父菜單ID不能為空")
    private String parentId;
 
    /**
     * 前端URL訪問(wèn)接口路徑
     */
    private String path;
 
    /**
     * 排序值
     */
    private Integer sort;
    /**
     * 創(chuàng)建時(shí)間
     */
 
    private LocalDateTime createTime;
 
    /**
     * 更新時(shí)間
     */
 
    private LocalDateTime updateTime;
 
    /**
     * 0--正常 1--刪除
     */
    private String delFlag;
 
    public Permission() {
        this.permissionId = IdUtil.simpleUUID();
    }

樹(shù)形結(jié)點(diǎn)類(lèi)

?
1
2
3
4
5
6
7
8
9
10
11
@Data
public class TreeNode {
    protected String id;
    protected String parentId;
    protected List<TreeNode> children = new ArrayList<TreeNode>();
    protected boolean hasChildren;
 
   public void addTreeNode(TreeNode node){
       children.add(node);
   }
}

樹(shù)形結(jié)點(diǎn)詳細(xì)信息類(lèi)

?
1
2
3
4
5
6
7
8
9
10
11
12
@Data
@EqualsAndHashCode(callSuper = true)
public class PermissionTree extends TreeNode implements Serializable {
    private String permissionName;
    private String permissionCode;
    private String path;
    private Integer sort;
    private String label;
    private boolean hasChildren;
    public PermissionTree() {
    }
}

構(gòu)建樹(shù)形結(jié)點(diǎn)工具類(lèi)(關(guān)鍵),在這里我用@UtilityClass注解就表示這個(gè)類(lèi)中的方法都是靜態(tài)方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@UtilityClass
public class TreeUtil {
 
 
    public <T extends TreeNode> List<T> build(List<T> treeNodes, String root) {
        List<T> trees = new ArrayList<>();
        for (T treeNode : treeNodes) {
 
            if (root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }
 
            for (T node : treeNodes) {
                if (node.getParentId().equals(treeNode.getId())) {
                    treeNode.addTreeNode(node);
                    treeNode.setHasChildren(true);
 
                }
            }
        }
        return trees;
    }
 
 
    /**
     * 通過(guò)permission創(chuàng)建樹(shù)形節(jié)點(diǎn)
     *
     * @param permissionList
     * @param root
     * @return
     */
    public List<PermissionTree> buildTree(List<Permission> permissionList, String root) {
        System.out.println(Arrays.toString(permissionList.toArray()));
        List<PermissionTree> treeNodeList = new ArrayList<>();
        PermissionTree treeNode = null;
        for (Permission permission : permissionList) {
            treeNode = new PermissionTree();
            treeNode.setId(permission.getPermissionId());
            treeNode.setPermissionName(permission.getPermissionName());
            treeNode.setPath(permission.getPath());
            treeNode.setSort(permission.getSort());
            treeNode.setParentId(permission.getParentId());
            treeNode.setLabel(permission.getPermissionName());
            treeNode.setHasChildren(false);
            treeNodeList.add(treeNode);
        }
        return TreeUtil.build(treeNodeList, root);
    }
  }

響應(yīng)消息主體類(lèi)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * 響應(yīng)信息主體
 *
 * @param <T>
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private int code;
 
    private String msg;
 
    private T data;
 
    public int getCode() {
        return code;
    }
 
    public void setCode(int code) {
        this.code = code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    public T getData() {
        return data;
    }
 
    public void setData(T data) {
        this.data = data;
    }
 
    public static <T> R<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);
    }
 
    public static <T> R<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);
    }
 
    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }
 
    public static <T> R<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }
 
    public static <T> R<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }
 
    public static <T> R<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }
 
    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }
 
    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}

數(shù)據(jù)查詢(xún)接口mapper類(lèi)

?
1
2
3
4
@Mapper
public interface PermissionMapper extends BaseMapper<Permission>{
    
}

數(shù)據(jù)邏輯處理業(yè)務(wù)接口

?
1
2
3
4
5
6
7
8
9
10
11
12
public interface PermissionService extends IService<Permission> {
  
 
    /**
     * 構(gòu)建權(quán)限樹(shù)
     *
     * @param lazy
     * @param parentId
     * @return
     */
    List<PermissionTree> treePermission(boolean lazy, String parentId);
}

數(shù)據(jù)邏輯處理業(yè)務(wù)接口實(shí)現(xiàn)類(lèi)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
/**
     * 構(gòu)建權(quán)限樹(shù):1、不是懶加載情況,查詢(xún)?nèi)?/code>
     * 2、是懶加載,根據(jù)parentId查詢(xún)
     *
     * @param lazy
     * @param parentId
     * @return
     */
 
    @Override
    public List<PermissionTree> treePermission(boolean lazy, String parentId) {
 
        if (!lazy) {
            return TreeUtil.buildTree(
                    baseMapper.selectList(Wrappers.<Permission>lambdaQuery().orderByAsc(Permission::getSort)),
                    CommonConstants.PERMISSION_ROOT_ID);
        }
        String parent = parentId == null ? CommonConstants.PERMISSION_ROOT_ID : parentId;
        return TreeUtil.buildTree(
                baseMapper.selectList(Wrappers.<Permission>lambdaQuery().eq(Permission::getParentId, parent).orderByAsc(Permission::getSort)), parent
        );
    }
}

查詢(xún)權(quán)限樹(shù)請(qǐng)求接口類(lèi)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowire
    private PermissionService permissionService;
 
    /**
     * 查詢(xún)權(quán)限列表,并以樹(shù)狀結(jié)構(gòu)顯示
     *
     * @param lazy 懶加載: false時(shí), parentId這個(gè)參數(shù)失效, 加載所有的權(quán)限; true時(shí), 根據(jù)parentId加載
     * @param parentId
     * @return
     */
    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    public R getTree(boolean lazy, String parentId) {
        return R.ok(permissionService.treePermission(lazy, parentId));
    }
 }

表中測(cè)試數(shù)據(jù)如下(注意它的parent_id

springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún)的方法

測(cè)試一:不是懶加載,查詢(xún)整個(gè)權(quán)限樹(shù)。 結(jié)果如下。

springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún)的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
    "code": 0,
    "msg": "SUCCESS",
    "data": [
        {
            "id": "1",
            "parentId": "-1",
            "children": [
                {
                    "id": "2",
                    "parentId": "1",
                    "children": [
                        {
                            "id": "3",
                            "parentId": "2",
                            "children": [],
                            "hasChildren": false,
                            "permissionName": "update",
                            "permissionCode": null,
                            "path": null,
                            "sort": 3,
                            "label": "update",
                            "owned": false
                        },
                        {
                            "id": "4",
                            "parentId": "2",
                            "children": [],
                            "hasChildren": false,
                            "permissionName": "insert_role",
                            "permissionCode": null,
                            "path": null,
                            "sort": 4,
                            "label": "insert_role",
                            "owned": false
                        }
                    ],
                    "hasChildren": true,
                    "permissionName": "delete",
                    "permissionCode": null,
                    "path": null,
                    "sort": 2,
                    "label": "delete",
                    "owned": false
                }
            ],
            "hasChildren": true,
            "permissionName": "add",
            "permissionCode": null,
            "path": null,
            "sort": 1,
            "label": "add",
            "owned": false
        },
        {
            "id": "5",
            "parentId": "-1",
            "children": [],
            "hasChildren": false,
            "permissionName": "role:saveRole",
            "permissionCode": null,
            "path": "/role/saveRole",
            "sort": 5,
            "label": "role:saveRole",
            "owned": false
        }
    ]
}

測(cè)試二:是懶加載,根據(jù)parent_id查詢(xún)當(dāng)前分支。 結(jié)果如下。

springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún)的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
    "code": 0,
    "msg": "SUCCESS",
    "data": [
        {
            "id": "3",
            "parentId": "2",
            "children": [],
            "hasChildren": false,
            "permissionName": "update",
            "permissionCode": null,
            "path": null,
            "sort": 3,
            "label": "update",
            "owned": false
        },
        {
            "id": "4",
            "parentId": "2",
            "children": [],
            "hasChildren": false,
            "permissionName": "insert_role",
            "permissionCode": null,
            "path": null,
            "sort": 4,
            "label": "insert_role",
            "owned": false
        }
    ]
}

到此這篇關(guān)于springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢(xún)的方法的文章就介紹到這了,更多相關(guān)springboot 樹(shù)形結(jié)構(gòu)并查詢(xún)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/to10086/article/details/109730685

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本人做受全过程视频 | 成品人视频免费观看 | 亚洲欧美日韩成人一区在线 | free哆拍拍免费永久视频 | 不卡一区二区三区 | 天天天做天天天天爱天天想 | 国产一线天 | 日韩在线观看一区二区不卡视频 | 国产视频一区二 | 韩国男女做性全过程视频 | 喜欢老头吃我奶躁我的动图 | 香蕉视频在线观看网站 | 亚洲国产福利精品一区二区 | 欧美不卡一区二区三区 | 99热国产在线观看 | 91精品综合 | 免费看国产一级特黄aa大片 | 国产精品日本一区二区不卡视频 | 亚洲图片一区二区三区 | 国产精品男人的天堂 | 91国内精品久久久久怡红院 | 深夜激情网站 | 欧美大片一级片 | 村妇超级乱淫伦小说全集 | 国产日韩欧美色视频色在线观看 | 国产原创精品 | 娇妻与老头绿文小说系列 | 特黄特色大片免费视频播放 | 亚洲欧美综合一区 | 高清女主播一区二区三区 | 欧美伊香蕉久久综合类网站 | 国产麻豆流白浆在线观看 | 国产精品一区二区三区免费视频 | 先锋资源久久 | 午夜DV内射一区区 | 99小视频| 幻女free性摘花第一次 | 四虎2020紧急免费入口 | 日本美女视频韩国视频网站免费 | 精品国产香蕉 | 国产成人亚洲影视在线 |