1、TreeUtil
传入一个list集合,根据父ID(pid)来判断是否有子节点,返回一个List类型
public class TreeUtil { private List<PermissionVo> menuCommon; /** * service层调用的方法,并将数据以list的形式返回 * * @param menu 菜单集合 * @return list */ public List<Object> menuList(List<PermissionVo> menu) { this.menuCommon = menu; List<Object> list = new ArrayList<>(); for (PermissionVo ps : menu) { Map<String, Object> map = new LinkedHashMap<>(); //判断根节点是否是0 if (ps.getPid().equals(0)) { setTreeData(map, ps); list.add(map); } } return list; } /** * 不判断根节点遍历 * * @param pList * @return */ public List<Object> userMenuList(List<PermissionVo> pList) { this.menuCommon = pList; List<Object> list = new ArrayList<>(); for (PermissionVo ps : pList) { Map<String, Object> map = new LinkedHashMap<>(); setTreeData(map, ps); list.add(map); } return list; } /** * 子集查找遍历 * 递归遍历,直到没有子节点为止 * * @param id 父id * @return list */ private List<Object> menuChild(Integer id) { List<Object> list = new ArrayList<>(); for (PermissionVo ps : menuCommon) { Map<String, Object> map = new LinkedHashMap<>(); if (ps.getPid().equals(id)) { setTreeData(map, ps); list.add(map); } } return list; } /** * 赋值 * id 当前id * title 标题 * field 权限值 * href 跳转路径 * children 子节点数据 */ private void setTreeData(Map<String, Object> map, PermissionVo ps) { map.put("id", ps.getId()); map.put("title", ps.getName()); map.put("field", ps.getValue()); map.put("href", ps.getUri()); map.put("children", menuChild(ps.getId())); } }
2、service层调用
从数据库查询一个list集合,调用TreeUtil工具类
@Override public Map<String, Object> findMenuTree(Integer type) { Map<String, Object> map = new HashMap<>(); //查询所有菜单 List<Permission> lists = new LambdaQueryChainWrapper<>(permissionMapper) .eq(Permission::getType, 0) .or() .eq(Permission::getType, type) .list(); TreeUtil menuTree = new TreeUtil(); List<PermissionVo> pList = new ArrayList<>(); //遍历赋值,拷贝 setPmsData(lists, pList); List<Object> menuList = menuTree.menuList(pList); map.put("list", menuList); return map; }
/** * 赋值 * @param list * @param pList */ private void setPmsData(List<Permission> list, List<PermissionVo> pList) { for (Permission p : list) { PermissionVo t = new PermissionVo(); t.setId(p.getId()); t.setName(p.getName()); t.setValue(p.getValue()); t.setUri(p.getUri()); t.setPid(p.getPid()); pList.add(t); } }
3、controller层返回
/** * 菜单生成树 */ @PreAuthorize("hasAuthority(‘method:pms:read‘)") @GetMapping("/findMenuTree") public CommonResult findMenuTree(Integer type) { Map<String, Object> map = permissionService.findMenuTree(type); return CommonResult.success(map); }
4、permission
permissionVo和permission字段差不多
@Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = false) public class Permission implements GrantedAuthority { private static final long serialVersionUID = 1L; /** * id */ @TableId(value = "id", type = IdType.AUTO) private Integer id; /** * 权限编号 */ private String code; /** * 父id 0根节点 */ private Integer pid; /** * 权限名 */ private String name; /** * 权限值 */ private String value; /** * 类型 1菜单 2方法 */ private Integer type; /** * 方法路径 */ private String uri; /** * 创建时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; /** * 是否可用 */ private String dataEnable; @Override public String getAuthority() { return this.value; }
1、TreeVo
@Data @AllArgsConstructor @NoArgsConstructor public class TreeVo implements Serializable { /** * id */ private Integer id; /** * 标题 */ private String title; /** * 权限值 */ private String field; /** * 跳转路径 */ private String href; /** * 子节点 */ private List<TreeVo> children; }
2、controller接收数据
/** * 分配菜单权限 */ @PreAuthorize("hasAuthority(‘method:pms:create‘)") @PostMapping("/getMenuTreeData") public CommonResult getMenuTreeData(@RequestBody List<TreeVo> treeVo, @RequestParam String roleCode, @RequestParam Integer type) { return permissionService.getMenuTreeData(treeVo, roleCode, type); }
3、service层解析树形结构数据
@Override public CommonResult getMenuTreeData(List<TreeVo> treeVo, String roleCode, Integer type) { //获取树节点Id List<Integer> pmsIds = new ArrayList<>(); for (TreeVo t : treeVo) { pmsIds.add(t.getId()); getChildrenId(t, pmsIds); } } /** * 递归遍历 直到没有子节点为止 * * @param t TreeVo * @param pmsIds list */ private void getChildrenId(TreeVo t, List<Integer> pmsIds) { for (TreeVo tr : t.getChildren()) { pmsIds.add(tr.getId()); getChildrenId(tr, pmsIds); } }
Java工具类 (2)------>TreeUtils------>树形结构生成类
原文:https://www.cnblogs.com/donleo123/p/14241967.html