1. Introduction
In some management systems, it is commonly used. Some tree data, such as department organization and authority, are used to generate tree data. Some tree data generation tools need to be written. Generally, recursive methods are used, and poor performance may lead to stack explosion. After analysis and reflection, I decided not to use recursive method to write tree data processing, and finally chose hasMap to maintain the relationship between tree nodes. Taking the permission tree as an example, this paper designs a tree data tool class.
2. Design of database tables
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for permission -- ---------------------------- DROP TABLE IF EXISTS `permission`; CREATE TABLE `permission` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Jurisdiction id', `name` varchar(32) NOT NULL COMMENT 'Name of permission', `url` varchar(64) DEFAULT NULL COMMENT 'Resources url', `type` int(11) NOT NULL COMMENT 'Permission type, 1:Module, 2: Menu, 3: url Resources', `parent_id` int(11) NOT NULL DEFAULT '0' COMMENT 'Superior resources id', `icon` varchar(64) DEFAULT NULL COMMENT 'Menu Icon', `sort` int(11) DEFAULT NULL COMMENT 'sort', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Update time', `operator` varchar(32) DEFAULT NULL COMMENT 'Operator', `level` int(11) NOT NULL DEFAULT '0', `code` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
3. Design of Java Bean
The design of Java beans is based on database tables, and lombok annotations are used to construct and get and set methods.
package com.lk.permission.common.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import lombok.experimental.Accessors; import java.util.ArrayList; import java.util.Date; import java.util.List; //@SuppressWarnings("serail") @ToString @NoArgsConstructor @AllArgsConstructor @Data @Accessors(chain = true) public class Permission { /** Permission ID */ private Integer id; /** Name of permission */ private String name; /** Permission encoding */ private String code; /** Menu Icon */ private String icon; /** Resource types */ private Integer type; /** Resource Address */ private String url; /** Hierarchy */ private Integer level; /** Upper ID */ private Integer parentId; /** sort */ private Integer sort; /** Creation time */ private Date createTime; /** Update time */ private Date updateTime; /** Operator */ private String operator; /** Subordinate authority */ private List<Permission> subPermissions = new ArrayList<>(); }
4. Tree Tool Class Design
I only provide a way of thinking, specifically according to their own business implementation, through hasMap mode, one is that the performance has been greatly improved, the other does not need to worry about the risk of stack explosion. The following maintenance relationship has been given, can be appropriate transformation to achieve their specific business needs.
package com.lk.permission.system.service.impl; import com.lk.permission.common.pojo.Permission; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class TreeService { private Map<Integer,Permission> map; private List<Permission> permissions; /** * Initialization of multitree * @param permissions */ TreeService(List<Permission> permissions){ this.permissions = permissions; this.map = new HashMap<>(); for (Permission permission : permissions){ this.map.put(permission.getId(),permission); } createTree(); } /** * Create a multifork tree */ private void createTree(){ for (Permission permission : this.permissions){ if (this.map.containsKey(permission.getParentId())){ this.map.get(permission.getParentId()).getSubPermissions().add(permission); System.out.println(permission.toString()); } } } /** * Acquisition of multifork trees based on hierarchy * @param level * @return */ List<Permission> getPermissionsByLevel(Integer level){ return this.permissions.parallelStream().filter(permission -> permission.getLevel() == level).collect(Collectors.toList()); } /** * Getting a multi-branch tree based on the id of the tree * @param id * @return */ Permission getPermissionById(Integer id){ return this.map.get(id); } /** * Adding child nodes to a multitree * @param permission */ public void addChild(Permission permission){ if (this.map.containsKey(permission.getParentId())){ ((Permission)this.map.get(permission.getParentId())).getSubPermissions().add(permission); } this.map.put(permission.getId(),permission); } }
5. Some test results achieved
- Privilege Selection
- TreeGrid