因為項目需要,頁面上需要樹形結(jié)構(gòu)的數(shù)據(jù)進行展示(類似下圖這樣),因此需要后端返回相應格式的數(shù)據(jù)。
不說廢話,直接開干!!!
我這里用的是springboot+mybatis-plus+mysql,示例的接口是查詢一級權(quán)限以及二級權(quán)限、三級權(quán)限整個權(quán)限樹…
下面是導入的maven依賴
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ù)庫連接--> < 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增強工具--> < 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 > |
下面是實體類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)限名稱不能為空" ) private String permissionName; /** * 權(quán)限標識 */ @NotNull (message = "權(quán)限標識不能為空" ) private String permissionCode; /** * 父菜單ID,如果是-1就表示是一級權(quán)限菜單。 */ @NotBlank (message = "父菜單ID不能為空" ) private String parentId; /** * 前端URL訪問接口路徑 */ private String path; /** * 排序值 */ private Integer sort; /** * 創(chuàng)建時間 */ private LocalDateTime createTime; /** * 更新時間 */ private LocalDateTime updateTime; /** * 0--正常 1--刪除 */ private String delFlag; public Permission() { this .permissionId = IdUtil.simpleUUID(); } |
樹形結(jié)點類
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); } } |
樹形結(jié)點詳細信息類
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)建樹形結(jié)點工具類(關(guān)鍵),在這里我用@UtilityClass
注解就表示這個類中的方法都是靜態(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; } /** * 通過permission創(chuàng)建樹形節(jié)點 * * @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); } } |
響應消息主體類
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
|
/** * 響應信息主體 * * @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ù)查詢接口mapper類
1
2
3
4
|
@Mapper public interface PermissionMapper extends BaseMapper<Permission>{ } |
數(shù)據(jù)邏輯處理業(yè)務接口
1
2
3
4
5
6
7
8
9
10
11
12
|
public interface PermissionService extends IService<Permission> { /** * 構(gòu)建權(quán)限樹 * * @param lazy * @param parentId * @return */ List<PermissionTree> treePermission( boolean lazy, String parentId); } |
數(shù)據(jù)邏輯處理業(yè)務接口實現(xià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
|
@Service public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService { /** * 構(gòu)建權(quán)限樹:1、不是懶加載情況,查詢?nèi)?/code> * 2、是懶加載,根據(jù)parentId查詢 * * @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 ); } } |
查詢權(quán)限樹請求接口類
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; /** * 查詢權(quán)限列表,并以樹狀結(jié)構(gòu)顯示 * * @param lazy 懶加載: false時, parentId這個參數(shù)失效, 加載所有的權(quán)限; true時, 根據(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)); } } |
表中測試數(shù)據(jù)如下(注意它的parent_id)
測試一:不是懶加載,查詢整個權(quán)限樹。 結(jié)果如下。
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 } ] } |
測試二:是懶加載,根據(jù)parent_id查詢當前分支。 結(jié)果如下。
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)造樹形結(jié)構(gòu)數(shù)據(jù)并查詢的方法的文章就介紹到這了,更多相關(guān)springboot 樹形結(jié)構(gòu)并查詢內(nèi)容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/to10086/article/details/109730685