public List<TreeSelect> buildMenuTreeSelect(List<PrivilegeDTO> menus) { List<PrivilegeDTO> menuTrees = new ArrayList<>(); List<Integer> tempList = menus.stream().map(PrivilegeDTO::getId).collect(Collectors.toList()); for (PrivilegeDTO menu : menus) { // 如果是顶级节点, 遍历该父节点的所有子节点 if (!tempList.contains(menu.getParentId())) { recursionFn(menus, menu); menuTrees.add(menu); } } if (menuTrees.isEmpty()) { menuTrees = menus; } return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList()); } /** * 递归列表,风暴大脑 * * @param list 分类表 * @param child 子节点 */ private void recursionFn(List<PrivilegeDTO> list, PrivilegeDTO child) { // 得到子节点列表 List<PrivilegeDTO> childList = getChildList(list, child); child.setChildren(childList); for (PrivilegeDTO p : childList) { //判断是否有子节点 if (!getChildList(list, p).isEmpty()) { recursionFn(list, p); } } } /** * 得到子节点列表 */ private List<PrivilegeDTO> getChildList(List<PrivilegeDTO> list, PrivilegeDTO child) { List<PrivilegeDTO> tlist = new ArrayList<>(); for (PrivilegeDTO privilegeDTO : list) { if (Objects.equals(privilegeDTO.getParentId(), child.getId())) { tlist.add(privilegeDTO); } } return tlist; }
附实体treeSelect结构文章来源地址https://www.toymoban.com/news/detail-686726.html
public class TreeSelect implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty("id") private Integer id; @ApiModelProperty("权限名称") private String name; @ApiModelProperty("子权限列表") //此注解可以使空值时此属性不返回于前端 @JsonInclude(JsonInclude.Include.NON_EMPTY) private List<TreeSelect> children; public TreeSelect(PrivilegeDTO menu) { this.id = menu.getId(); this.name = menu.getName(); this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList()); } }
文章来源:https://www.toymoban.com/news/detail-686726.html
到了这里,关于后台菜单权限实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!