目录树结构

2022-06-14 15:23:58 浏览数 (1)

实际项目中后端生成目录树的json是比较常见的,hutool中提供了treeUtil使用起来比较方便。先做个记录

通过hutool

引入hutool的jar

代码语言:javascript复制
   <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>
代码语言:javascript复制
  public List<Tree<String>> findOrgTreeInfoByUserCode() {
       
//获取数据  设定根节点的parentId为:0
List<OrgTreeNode> orgTreeNodes = customerBoardMapper.findOrgTrees();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setChildrenKey("children");

        //可配置树深度
//        treeNodeConfig.setDeep(2);
        treeNodeConfig.setIdKey("id");
        //转换器
        List<Tree<String>> build = TreeUtil.build(treeNodesList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setName(treeNode.getName());

                });

        return build;


    }
代码语言:javascript复制
@Data
public class OrgTreeNode {

    private String id;

    private String parentId;

    private String name;

    private String belongRegionId;
    private String belongRegionName;
    private List<OrgTreeNode> children = new ArrayList<>();

   
}

小测试demo

代码语言:javascript复制
    @GetMapping("menu")
   public String getTreeMenu(){
       List<Menu> list = helloService.findMenu();
       List<Menu> result =new ArrayList<>();
        Map<Integer, Menu> menuMap = new HashMap<>();

       for(Menu menu:list){

           if(menu.getParentId()==0){
               Menu menu1 =new Menu();
                menu1.setId(menu.getId());
                menu1.setName(menu.getName());
               result.add(menu1);
               menuMap.put(menu.getId(), menu1);

           }else{
               if(menuMap.containsKey(menu.getParentId())){
                   Menu menu2 = new Menu();
                   menu2.setId(menu.getId());
                   menu2.setParentId(menu.getParentId());
                   menu2.setName(menu.getName());
                   menuMap.get(menu.getParentId()).getChildren().add(menu2);
                   menuMap.put(menu.getId(), menu2);

               }

           }
       }
       return JSON.toJSONString(result);
   }
代码语言:javascript复制
@Data
public class Menu {

    private Integer id;
    private Integer parentId;
    private String name;
    private List<Menu> children = new ArrayList<>();


}

效果展示(这里的排序还需要处理)

代码语言:javascript复制
[{
	"children": [{
		"children": [{
			"children": [],
			"id": 133,
			"name": "添加配置",
			"parentId": 24
		}, {
			"children": [],
			"id": 134,
			"name": "添加配置",
			"parentId": 24
		}],
		"id": 24,
		"name": "配置管理",
		"parentId": 8
	}, {
		"children": [{
			"children": [],
			"id": 132,
			"name": "日志删除",
			"parentId": 25
		}],
		"id": 25,
		"name": "日志管理",
		"parentId": 8
	}, {
		"children": [{
			"children": [],
			"id": 119,
			"name": "查看",
			"parentId": 45
		}, {
			"children": [],
			"id": 126,
			"name": "新增",
			"parentId": 45
		}, {
			"children": [],
			"id": 127,
			"name": "修改",
			"parentId": 45
		}, {
			"children": [],
			"id": 131,
			"name": "禁用启用",
			"parentId": 45
		}],
		"id": 45,
		"name": "用户管理",
		"parentId": 8
	}, {
		"children": [{
			"children": [],
			"id": 115,
			"name": "查看",
			"parentId": 46
		}, {
			"children": [],
			"id": 116,
			"name": "新增",
			"parentId": 46
		}, {
			"children": [],
			"id": 117,
			"name": "修改",
			"parentId": 46
		}, {
			"children": [],
			"id": 118,
			"name": "删除",
			"parentId": 46
		}],
		"id": 46,
		"name": "菜单管理",
		"parentId": 8
	}, {
		"children": [{
			"children": [],
			"id": 120,
			"name": "查看",
			"parentId": 47
		}, {
			"children": [],
			"id": 128,
			"name": "新增",
			"parentId": 47
		}, {
			"children": [],
			"id": 129,
			"name": "修改",
			"parentId": 47
		}, {
			"children": [],
			"id": 130,
			"name": "删除",
			"parentId": 47
		}],
		"id": 47,
		"name": "角色管理",
		"parentId": 8
	}, {
		"children": [],
		"id": 48,
		"name": "组织管理",
		"parentId": 8
	}],
	"id": 8,
	"name": "系统管理"
}]

说明

JSON.toJSONString(result);需要引入fastjson的jar

代码语言:javascript复制
  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
        </dependency>

0 人点赞