数据库的树状储存转为java的树表
学习目标:
保存代码用
学习内容:
Mytree类
package com.ruoyi.common.core.domain;
import java.util.List;
public class MyTree{
/** 节点ID */
private Long id;
/** 节点父ID */
private Long pId;
/** 节点名称 */
private String name;
/** 节点标题 */
private String title;
/** 父id **/
private Long parentId;
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public Long getParentId() {
return parentId;
}
private List<MyTree> children;
public void setId(Long id) {
this.id = id;
}
public void setpId(Long pId) {
this.pId = pId;
}
public void setName(String name) {
this.name = name;
}
public void setTitle(String title) {
this.title = title;
}
public String setChildren(List<MyTree> children) {
this.children = children;
return "123";
}
public Long getId() {
return id;
}
public Long getpId() {
return pId;
}
public String getName() {
return name;
}
public String getTitle() {
return title;
}
public List<MyTree> getChildren() {
return children;
}
@Override
public String toString() {
return "MyTree{" +
"id=" + id +
", pId=" + pId +
", name='" + name + '\'' +
", title='" + title + '\'' +
", children=" + children +
'}';
}
}
service类(重组树结构方法,递归)
public List<MyTree> CombinationTree(Long parentId, List<MyTree> list) {
List<MyTree> ProjectList1 = new ArrayList<>();
// 为一级项目添加内容
ProjectList1= list.stream()
.filter(pro -> pro.getpId() == parentId)
.collect(Collectors.toList());
if (ProjectList1.size() == 0) {
return null;
}
// 为二级项目添加内容,添加一级项目中list的内容
ProjectList1.stream()
.forEach(child ->child.setChildren(CombinationTree(child.getId(), list)));
return ProjectList1;
}
不断地循环去过滤出和输入parentId相等的列,不存在即为最后节点,递归返回