@Data
public class Department {
private Long id;
private String name;
private Long parentId;
}
2、返回前段数据格式实体
@Data
public class DepartmentDTO {
private Long id;
private String name;
private Long parentId;
private List<DepartmentDTO> children = new ArrayList<>();
}
3、主调用方法
private static List<DepartmentDTO> makeTree(List<DepartmentDTO> departmentList, Long pId) {
//子类
List<DepartmentDTO> children = departmentList.stream().filter(x -> x.getParentCode()==pId).collect(Collectors.toList());
//后辈中的非子类
List<DepartmentDTO> successor = departmentList.stream().filter(x -> x.getParentCode()!=pId).collect(Collectors.toList());
children.forEach(x ->{
makeTree(successor, x.getDepartmentId()).forEach(
y -> x.getChildren().add(y)
);
}
);
return children;
}
1、数据库请求数据返回实体@Datapublic class Department { private Long id; private String name; private Long parentId;}2、返回前段数据格式实体@Datapublic class DepartmentDTO { private Long id;...