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

树的方法

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>
相关推荐
一水鉴天4 小时前
整体设计 定稿 之9 最后收束 app.py: 应用项目的结构及其模型和框架 (豆包助手)
服务器·windows·microsoft
wanhengidc4 小时前
云手机的适配性怎么样?
运维·服务器·安全·智能手机·云计算
小王师傅664 小时前
【轻松入门SpringBoot】actuator健康检查(上)
java·spring boot·后端
梁辰兴4 小时前
计算机网络基础:使用集线器的星型拓扑
服务器·网络·计算机网络·集线器·计算机网络基础·梁辰兴·星型拓扑
jimy14 小时前
安卓里运行Linux
linux·运维·服务器
醒过来摸鱼4 小时前
Java classloader
java·开发语言·python
专注于大数据技术栈4 小时前
java学习--StringBuilder
java·学习
loosenivy5 小时前
企业银行账户归属地查询接口如何用Java调用
java·企业银行账户归属地·企业账户查询接口·企业银行账户查询
咕噜签名-铁蛋5 小时前
PyTorch:深度学习框架的创新之路与技术实践
服务器
IT 行者5 小时前
Spring Security 6.x 迁移到 7.0 的完整步骤
java·spring·oauth2