获取字典树形结构框架树代码

树的方法

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>
相关推荐
小吕学编程1 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
weixin_438335407 分钟前
Spring Boot实现接口时间戳鉴权
java·spring boot·后端
知之则吱吱35 分钟前
亚马逊AWS云服务器高效使用指南:最大限度降低成本的实战策略
服务器·云计算·aws
pan_junbiao39 分钟前
Spring框架的设计模式
java·spring·设计模式
远方160940 分钟前
0x-2-Oracle Linux 9上安装JDK配置环境变量
java·linux·oracle
北执南念1 小时前
CompletableFuture+线程池使用案列
java
cui_win1 小时前
每日一令:Linux 极简通关指南 - 汇总
linux·运维·服务器
知星小度S1 小时前
Linux权限探秘:驾驭权限模型,筑牢系统安全
linux·运维·服务器
黄交大彭于晏1 小时前
发送文件脚本源码版本
java·linux·windows
钮钴禄·爱因斯晨1 小时前
Java 面向对象进阶之多态:从概念到实践的深度解析
java·开发语言·数据结构