树的方法
java
package org.springjmis.core.tool.node;
import java.util.List;
public class ForestNodeMerger {
public ForestNodeMerger() {
}
public static <T extends INode> List<T> merge(List<T> items) {
ForestNodeManager<T> forestNodeManager = new ForestNodeManager(items);
items.forEach((forestNode) -> {
if (forestNode.getParentId() != 0L) {
T node = forestNodeManager.getTreeNodeAT(forestNode.getParentId());
if (node != null) {
node.getChildren().add(forestNode);
} else {
forestNodeManager.addParentId(forestNode.getId());
}
}
});
return forestNodeManager.getRoot();
}
}
新建一个node接口
java
import java.io.Serializable;
import java.util.List;
public interface INode extends Serializable {
Long getId();
Long getParentId();
List<INode> getChildren();
default Boolean getHasChildren() {
return false;
}
}
在实体里面实现node的方法
java
//在实体里面实现node的方法
public class AimDatasetElementsVo extends AimDatasetElements implements INode {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
/**
* 父节点ID
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long parentId;
/**
* 子孙节点
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<INode> children;
@Override
public Long getParentId() {
return null;
}
@Override
public List<INode> getChildren() {
if (this.children == null) {
this.children = new ArrayList<>();
}
return this.children;
}
controller层
java
/**
* 获取字典树形结构
*
* @return
*/
@GetMapping("/tree")
@ApiOperationSupport(order = 3)
@ApiOperation(value = "树形结构", notes = "树形结构")
public R<List<DictVO>> tree() {
List<DictVO> tree = dictService.tree();
return R.data(tree);
}
service层
java
/**
* 树形结构
*
* @return
*/
List<DictVO> tree();
service实现层
java
@Override
public List<DictVO> tree() {
return ForestNodeMerger.merge(baseMapper.tree());
}
mapper层
java
/**
* 获取树形节点
*
* @return
*/
List<DictVO> tree();
mapper.xml层
java
<resultMap id="treeNodeResultMap" type="org.springjmis.core.tool.node.TreeNode">
<id column="id" property="id"/>
<result column="parent_id" property="parentId"/>
<result column="title" property="title"/>
<result column="value" property="value"/>
<result column="key" property="key"/>
</resultMap>
<select id="tree" resultMap="treeNodeResultMap">
select id, parent_id, dict_value as title, id as 'value', id as 'key' from jmis_dict where is_deleted = 0
</select>