目 录CONTENT

文章目录

目录树结构

在水一方
2022-04-10 / 0 评论 / 0 点赞 / 422 阅读 / 5,412 字 / 正在检测是否收录...

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

通过hutool

引入hutool的jar

   <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>

  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;


    }




样例

    public ResponseResult<List<Tree<Long>>> test(String name,int type)  {
        List<BasicParamResponseVO> treeNodesList = new ArrayList<>();

        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.like("project_name", name);
//        wrapper.eq("is_classify", type);
        List<BeStarItemsGroup> li = beStarItemsGroupMapper.selectList(wrapper);
        li.forEach(k->{
            BasicParamResponseVO v = new BasicParamResponseVO();
            v.setId(k.getId());
            v.setParentId(k.getParentId());
            v.setProjectName(k.getProjectName());
            v.setType(k.getIndexType());
            treeNodesList.add(v);
        });
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        // 排序的字段
        treeNodeConfig.setWeightKey("id");
        List<Tree<Long>> build = TreeUtil.build(treeNodesList, 0L, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setName(treeNode.getProjectName());
//                    tree.putExtra("type",treeNode.getType());
                    tree.put("type", treeNode.getType());

                });
        return ResponseResult.succeed(build);
@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

    @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);
   }


@Data
public class Menu {

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


}

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


[{
	"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

  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
        </dependency>

0

评论区