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

树的方法

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>
相关推荐
L1624762 分钟前
Windows 系统下 ZIP安装MySQL 详细操作步骤
数据库·windows·mysql
毕设源码-钟学长8 分钟前
【开题答辩全过程】以 基于java旅游网站的设计与实现为例,包含答辩的问题和答案
java·开发语言·旅游
我先测了22 分钟前
零第三方工具!Win10 自带磁盘管理给 C 盘扩容「纯官方」操作笔记
windows·笔记·扩容·c盘扩容
杰克崔24 分钟前
glibc社区提问
linux·运维·服务器·车载系统
zs宝来了31 分钟前
ArrayList源码阅读
java
wqdian_com36 分钟前
中文域名的准确展示能否堵住网络钓鱼攻击“后门”?
服务器·安全·php
brave_zhao1 小时前
关于javafx下打开postman无法正常关闭postman的问题
java·测试工具·postman
代码游侠1 小时前
应用——Web服务器项目代码解析
运维·服务器·开发语言·前端·笔记·html
攻心的子乐1 小时前
Spring IOC 源码
java·后端·spring
Sirens.1 小时前
Java异常处理解析:从防御式编程到自定义异常类
java·开发语言·笔记·学习·github·javac