public List<DeptVO> tree(String tenantId) { return ForestNodeMerger.merge(baseMapper.tree(tenantId)); }baseMapper.tree(tenantId)为目标数据源
ForestNodeMerger 工具类
import java.util.List;
public class ForestNodeMerger {
public ForestNodeMerger() {
}
public static <T extends INode<T>> List<T> merge(List<T> items) {
ForestNodeManager<T> forestNodeManager = new ForestNodeManager(items);
items.forEach((forestNode) -> {
if (forestNode.getParentId() != 0L) {
INode<T> node = forestNodeManager.getTreeNodeAt(forestNode.getParentId());
if (node != null) {
node.getChildren().add(forestNode);
} else {
forestNodeManager.addParentId(forestNode.getId());
}
}
});
return forestNodeManager.getRoot();
}
}
ForestNodeManager 工具类
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ForestNodeManager<T extends INode<T>> {
private final ImmutableMap<Long, T> nodeMap;
private final Map<Long, Object> parentIdMap = Maps.newHashMap();
public ForestNodeManager(List<T> nodes) {
this.nodeMap = Maps.uniqueIndex(nodes, INode::getId);
}
public INode<T> getTreeNodeAt(Long id) {
return this.nodeMap.containsKey(id) ? (INode)this.nodeMap.get(id) : null;
}
public void addParentId(Long parentId) {
this.parentIdMap.put(parentId, "");
}
public List<T> getRoot() {
List<T> roots = new ArrayList();
this.nodeMap.forEach((key, node) -> {
if (node.getParentId() == 0L || this.parentIdMap.containsKey(node.getId())) {
roots.add(node);
}
});
return roots;
}
}
INode<T> 接口
import java.io.Serializable;
import java.util.List;
public interface INode<T> extends Serializable {
Long getId();
Long getParentId();
List<T> getChildren();
default Boolean getHasChildren() {
return false;
}
}
DeptVO 要继承INode接口
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "DeptVO对象", description = "DeptVO对象")
public class DeptVO extends Dept implements INode<DeptVO> {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
//这三个为INode里的属性
/**
* 父节点ID
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long parentId;
/**
* 子孙节点
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<DeptVO> children;
/**
* 是否有子孙节点
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Boolean hasChildren;
@Override
public List<DeptVO> getChildren() {
if (this.children == null) {
this.children = new ArrayList<>();
}
return this.children;
}
/**
* 上级机构
*/
private String parentName;
/**
* 机构类型名称
*/
private String deptCategoryName;
该代码段展示了如何使用ForestNodeMerger工具类和ForestNodeManager类将基于INode接口的DeptVO部门对象进行树形结构组织。ForestNodeMerger通过遍历并根据父节点ID将节点添加到相应父节点的子节点列表中,最终返回根节点列表。这个过程涉及到数据源的转换和树结构的构建,对于数据管理和组织特别有用。
882

被折叠的 条评论
为什么被折叠?



