编辑
2023-04-20
实用工具
00
请注意,本文编写于 714 天前,最后修改于 714 天前,其中某些信息可能已经过时。

目录

简介
步骤
pom
controller
sevice
seviceImpl
结果

简介

项目中经常会遇到各种需要以树形结构展示的功能,如菜单树、分类树、部门树,Hutool的TreeUtil主要是用来快速构造树形结构,以及获取所有叶子节点等操作。🍉

步骤

  1. 引入hutool最新pom包。
  2. 获取构造树的分类数据。
  3. TreeNodeConfig信息配置,配置节点名称、孩子节点key信息、排序等等。
  4. 调用TreeUtil.build()构造树。

pom

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

controller

java
/** * 获取构造树 **/ @ApiOperation(value = "获取构造树", notes = "获取构造树") @GetMapping(value = "/getConstructTree") public Result<?> getConstructTree() { List<Tree<String>> treeList = sdSchoolClassTypeDataCategoryService.constructTree(); return Result.OK(treeList); }

sevice

java
/** * 获取构造树 * **/ @ApiOperation(value = "获取构造树", notes = "获取构造树") @GetMapping(value = "/getConstructTree") public Result<?> getConstructTree() { List<Tree<String>> treeList = sdSchoolClassTypeDataCategoryService.constructTree(); return Result.OK(treeList); }

seviceImpl

java
@Override public List<Tree<String>> constructTree() { //1.获取所有资料分类 List<SdSchoolClassTypeDataCategory> dataList = this.lambdaQuery().getBaseMapper().selectList(Wrappers.lambdaQuery(SdSchoolClassTypeDataCategory.class) .eq(SdSchoolClassTypeDataCategory::getStatus, SchoolConstant.ENABLE_STATUS) .eq(SdSchoolClassTypeDataCategory::getDeleted, SchoolConstant.DELETE_STATUS_NORMAL)); //2.配置 TreeNodeConfig config = new TreeNodeConfig(); config.setIdKey("id"); //默认id,可以不设置 config.setParentIdKey("pid"); //父id config.setNameKey("dataCategoryName"); //分类名称 config.setDeep(3); //最大递归深度 config.setChildrenKey("childrenList"); //孩子节点 config.setWeightKey("sort"); //排序字段 //3.转树 List<Tree<String>> treeList = TreeUtil.build(dataList, "0", config, ((object, treeNode) -> { treeNode.putExtra("id", object.getId()); treeNode.putExtra("pid", object.getPid()); treeNode.putExtra("dataCategoryName", object.getDataCategoryName()); treeNode.putExtra("level", object.getLevel()); treeNode.putExtra("sort", object.getSort()); //扩展属性... })); return treeList; }

结果

java
{     "success":true,     "message":"操作成功!",     "code":200,     "result":[ {             "id":"1447031605584797698",             "pid":"0",             "dataCategoryName":"开发测试资料一级分类",             "level":1,             "sort":1,             "childrenList":[                 {                     "id":"1447031722601684993",                     "pid":"1447031605584797698",                     "dataCategoryName":"开发测试资料二级分类",                     "level":2,                     "sort":1,                     "childrenList":[                         {                             "id":"1516684508672299010",                             "pid":"1447031722601684993",                             "dataCategoryName":"开发测试资料三级分类",                             "level":3,                             "sort":1                         }                     ]                 }             ]         },         {             "id":"1447849327826636801",             "pid":"0",             "dataCategoryName":"测试资料分类",             "level":1,             "sort":1,             "childrenList":[                 {                     "id":"1447849471787732993",                     "pid":"1447849327826636801",                     "dataCategoryName":"测试资料分类2-1",                     "level":2,                     "sort":1                 },                 {                     "id":"1447849472085528577",                     "pid":"1447849327826636801",                     "dataCategoryName":"测试资料分类2-2",                     "level":2,                     "sort":1                 },                 {                     "id":"1447849472219746305",                     "pid":"1447849327826636801",                     "dataCategoryName":"测试资料分类2-3",                     "level":2,                     "sort":1                 }             ]         }     ] }

本文作者:Weee

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!