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

树的方法

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>
相关推荐
heartbeat..3 分钟前
介绍一下软件开发中常见的几种的架构模式
java·架构·开发
天天摸鱼的小学生5 分钟前
【Java Enum枚举】
java·开发语言
癫狂的兔子12 分钟前
【Office】【Excel】数据透视图
windows
爬山算法18 分钟前
Redis(168) 如何使用Redis实现会话管理?
java·数据库·redis
程语有云22 分钟前
生产事故-那些年遇到过的OOM
java·内存·oom·生产事故
雨中飘荡的记忆26 分钟前
Spring Test详解
java·后端·spring
橘颂TA26 分钟前
【Linux】System V 通信——共享内存
linux·运维·服务器·c++
天赐学c语言27 分钟前
Linux - 网络基础概念
linux·服务器·网络·socket
sugar__salt34 分钟前
网络编程套接字(二)——TCP
java·网络·网络协议·tcp/ip·java-ee·javaee