Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of constructing tree structure data and querying by springboot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the method of constructing tree structure data and querying by springboot". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the method of springboot constructing tree structure data and querying"?

Because the project needs, the data in the tree structure needs to be displayed on the page (similar to the figure below), so the back-end needs to return the data in the corresponding format.

What I use here is springboot+mybatis-plus+mysql, the interface of the example is to query the first-level permission and the second-level permission, the third-level permission of the whole permission tree.

Here are the imported maven dependencies

Org.springframework.boot spring-boot-starter-web com.alibaba druid-spring-boot-starter 1.1.21 mysql mysql-connector-java com.baomidou mybatis-plus-boot-starter 3.4 . 0 org.projectlombok lombok true cn.hutool hutool-all 5.0.6

Here is the entity class Permission

@ Datapublic class Permission implements Serializable {@ TableId private String permissionId; @ NotNull (message = "permission name cannot be empty") private String permissionName; / * * permission ID * / @ NotNull (message = "permission ID cannot be empty") private String permissionCode; / * parent menu ID. If it is-1, it is a first-level permission menu. * / @ NotBlank (message = "parent menu ID cannot be empty") private String parentId; / * frontend URL provider path * / private String path; / * * sort value * / private Integer sort; / * creation time * / private LocalDateTime createTime; / * Update time * / private LocalDateTime updateTime / * 0Mui-normal 1Mui-Delete * / private String delFlag; public Permission () {this.permissionId = IdUtil.simpleUUID ();}

Tree node class

@ Datapublic class TreeNode {protected String id; protected String parentId; protected List children = new ArrayList (); protected boolean hasChildren; public void addTreeNode (TreeNode node) {children.add (node);}}

Tree node detail class

@ 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 () {}}

Build the tree node utility class (key). Here I use the @ UtilityClass annotation to indicate that the methods in this class are static methods:

@ UtilityClasspublic class TreeUtil {public List build (List treeNodes, String root) {List 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 } / * create a tree node through permission * * @ param permissionList * @ param root * @ return * / public List buildTree (List permissionList, String root) {System.out.println (Arrays.toString (permissionList.toArray (); List 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);}}

Response message body class

/ * * response message body * * @ param * / @ ToString@NoArgsConstructor@AllArgsConstructor@Accessors (chain = true) public class R 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 R ok () {return restResult (null, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);} public static R ok (T data) {return restResult (data, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);} public static R ok (T data, String msg) {return restResult (data, CommonConstants.SUCCESS, msg) } public static R failed () {return restResult (null, CommonConstants.FAIL, null);} public static R failed (String msg) {return restResult (null, CommonConstants.FAIL, msg);} public static R failed (T data) {return restResult (data, CommonConstants.FAIL, null) } public static R failed (T data, String msg) {return restResult (data, CommonConstants.FAIL, msg);} private static R restResult (T data, int code, String msg) {R apiResult = new R (); apiResult.setCode (code); apiResult.setData (data); apiResult.setMsg (msg) Return apiResult;}}

Data query interface mapper class

@ Mapperpublic interface PermissionMapper extends BaseMapper {}

Data logic processing service interface

Public interface PermissionService extends IService {/ * build permission tree * * @ param lazy * @ param parentId * @ return * / List treePermission (boolean lazy, String parentId);}

Data logic processing business interface implementation class

@ Servicepublic class PermissionServiceImpl extends ServiceImpl implements PermissionService {/ * build permission tree: 1. Not lazy loading, query all * 2, lazy loading According to parentId query * * @ param lazy * @ param parentId * @ return * / @ Override public List treePermission (boolean lazy, String parentId) {if (! lazy) {return TreeUtil.buildTree (baseMapper.selectList (Wrappers.lambdaQuery (). OrderByAsc (Permission::getSort)), CommonConstants.PERMISSION_ROOT_ID) } String parent = parentId = = null? CommonConstants.PERMISSION_ROOT_ID: parentId; return TreeUtil.buildTree (baseMapper.selectList (Wrappers.lambdaQuery (). Eq (Permission::getParentId, parent) .orderByAsc (Permission::getSort)), parent);}}

Query permission tree request interface class

@ RestController@RequestMapping ("/ permission") public class PermissionController {@ Autowire private PermissionService permissionService; / * queries the permission list and displays * * @ param lazy lazy load: false in a tree structure, the parameter parentId is invalid and all permissions are loaded When true, load * @ param parentId * @ return * / @ RequestMapping (value = "/ getTree", method = RequestMethod.GET) public R getTree (boolean lazy, String parentId) {return R.ok (permissionService.treePermission (lazy, parentId));}} according to parentId

The test data in the table is as follows (note its parent_id)

Test 1: instead of lazy loading, query the entire permission tree. The results are as follows.

{"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}]}

Test 2: lazy load, query the current branch according to parent_id. The results are as follows.

{"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}]} so far I believe you have a deeper understanding of "what is the method for springboot to construct tree-structured data and query", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report