我们会经常用到树形,那么树形结构的数据是在前端做还是在后台做呢?我自己用过前端的ztree,selectTree等这些属于前端的组件,后台只需要把一个表的所有数据返回给前段就可以,前端可以通过id,pid来把层级结构划分,要是我们前端需要后台直接返回树结构数据怎么办,那么接下来我给大家介绍一下我写过的例子。
我们先看一张图了解一下树结构:我这里随便找一张图了解一下即可
接下来我们写一个小例子,用递归方式转换为数
实体:
package org.inlighting.handwritingjwt;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
import java.util.List;
/**
* 实体类
*
* @author Yujiaqi
* @date 2020/12/15 14:03
*/
@Data
@Getter
@Setter
@ToString
public class TestEntity implements Serializable {
private Integer id;
private String name;
private String parentId;
private List<TestEntity> children;
public TestEntity(){};
public TestEntity(Integer id, String name, String parentId, List<TestEntity> children) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.children = children;
}
}
service实现类
@RestController
public class Test {
private static final String json = "["
+ "{\"id\":44116841,\"name\":\"测试部\",\"parentId\":56188932},"
+ "{\"id\":56104925,\"name\":\"飞轮储能事业部\",\"parentId\":null},"
+ "{\"id\":56121882,\"name\":\"高速电机部\",\"parentId\":56104925},"
+ "{\"id\":44941198,\"name\":\"项目管理部\",\"parentId\":null},"
+ "{\"id\":35232631,\"name\":\"视觉算法\",\"parentId\":19693688},"
+ "{\"id\":17387338,\"name\":\"人力行政部\",\"parentId\":null},"
+ "{\"id\":35197588,\"name\":\"行政\",\"parentId\":17387338},"
+ "{\"id\":19693688,\"name\":\"人工智能部\",\"parentId\":56188932},"
+ "{\"id\":35193567,\"name\":\"人力资源\",\"parentId\":17387338},"
+ "{\"id\":56122856,\"name\":\"结构部\",\"parentId\":56104925},"
+ "{\"id\":54197060,\"name\":\"系统平台部\",\"parentId\":56188932},"
+ "{\"id\":17387341,\"name\":\"财务部\",\"parentId\":null},"
+ "{\"id\":56188932,\"name\":\"机器人事业部\",\"parentId\":null},"
+ "{\"id\":35203611,\"name\":\"自然语言算法\",\"parentId\":19693688}]";
@RequestMapping("/test")
public List<TestEntity> list(){
List<TestEntity> testEntitys = JSON.parseObject(json, new TypeReference<List<TestEntity>>(){});
return TreeUtils.buildByRecursive(testEntitys);
}
递归方法工具类
package org.inlighting.handwritingjwt;
import java.util.ArrayList;
import java.util.List;
/**
* @author 于嘉琪
*/
public class TreeUtils {
/**
* 使用递归方法建树
* @param
* @return
*/
public static List<TestEntity> buildByRecursive(List<TestEntity> testEntities) {
List<TestEntity> trees = new ArrayList<>();
for (TestEntity testEntity : testEntities) {
if ("".equals(testEntity.getParentId()) || testEntity.getParentId() == null ) {
trees.add(findChildren(testEntity,testEntities));
}
}
return trees;
}
/**
* 递归查找子节点
* @param
* @return
*/
public static TestEntity findChildren(TestEntity testEntity,List<TestEntity> testEntitys) {
for (TestEntity projectBasicInfoDTO2 : testEntitys) {
if(String.valueOf(testEntity.getId()).equals(projectBasicInfoDTO2.getParentId())) {
if(testEntity.getChildren() == null) {
testEntity.setChildren(new ArrayList<>());
}
//是否还有子节点,如果有的话继续往下遍历,如果没有则直接返回
testEntity.getChildren().add(findChildren(projectBasicInfoDTO2,testEntitys));
}
}
return testEntity;
}
}
查出来的效果
[
{
"id": 56104925,
"name": "飞轮储能事业部",
"parentId": null,
"children": [
{
"id": 56121882,
"name": "高速电机部",
"parentId": "56104925",
"children": null
},
{
"id": 56122856,
"name": "结构部",
"parentId": "56104925",
"children": null
}
]
},
{
"id": 44941198,
"name": "项目管理部",
"parentId": null,
"children": null
},
{
"id": 17387338,
"name": "人力行政部",
"parentId": null,
"children": [
{
"id": 35197588,
"name": "行政",
"parentId": "17387338",
"children": null
},
{
"id": 35193567,
"name": "人力资源",
"parentId": "17387338",
"children": null
}
]
},
{
"id": 17387341,
"name": "财务部",
"parentId": null,
"children": null
},
{
"id": 56188932,
"name": "机器人事业部",
"parentId": null,
"children": [
{
"id": 44116841,
"name": "测试部",
"parentId": "56188932",
"children": null
},
{
"id": 19693688,
"name": "人工智能部",
"parentId": "56188932",
"children": [
{
"id": 35232631,
"name": "视觉算法",
"parentId": "19693688",
"children": null
},
{
"id": 35203611,
"name": "自然语言算法",
"parentId": "19693688",
"children": null
}
]
},
{
"id": 54197060,
"name": "系统平台部",
"parentId": "56188932",
"children": null
}
]
}
]
以上为后台处理树结构小demo,重点为递归那个工具类