使用hutool工具生成树形结构

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。使用hutool工具生成树形结构,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

假设要构建一个菜单,可以实现智慧库房,菜单的样子如下:

智慧库房
    |- RFID
    |- 智慧大屏
    |- 智能密集架
    |- 环境管控

那这种结构如何保存在数据库中呢?一般是这样的:

​ 每条数据根据parentId相互关联并表示层级关系,parentId在这里也叫外键

id parentId name
1 0 智慧库房
2 1 RFID
3 1 智慧大屏
4 1 智能密集架
5 1 环境管控

使用步骤:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

我这里使用的是枚举:(大家可以使用数据将需要封装成树形结构的数据,查询封装成一个List)

package com.hy.archive.supervision.enums;

/**
 * @description: 智慧库房功能
 */

public enum SmartWarehouseFunctionEnum {

    SMART_WAREHOUSE("1", "智慧库房", "0"),
    RFID("2", "RFID", "1"),
    SMART_LARGE_SCREEN("3", "智慧大屏", "1"),
    INTELLIGENT_DENSE_RACK("4", "智能密集架", "1"),
    ENVIRONMENTAL_CONTROL("5", "环境管控", "1");


    private String id;
    private String name;
    private String pid;


    SmartWarehouseFunctionEnum(String id, String name, String pid) {
        this.id = id;
        this.name = name;
        this.pid = pid;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPid() {
        return pid;
    }


    public static SmartWarehouseFunctionEnum fromCode(String id) {
        for (SmartWarehouseFunctionEnum DispatchTypeEnum : SmartWarehouseFunctionEnum.values()) {
            if (DispatchTypeEnum.getId().equals(id)) {
                return DispatchTypeEnum;
            }
        }
        return null;
    }
}

package com.js.hutool.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.js.hutool.enums.SmartWarehouseFunctionEnum;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/frid")
public class FridFunctionController {

    /**
     * @description: 使用htool工具生成树形递归结构
     * @author: js
     * @date: 2023/8/4 15:36
     * @param:
     * @return:
     **/
    @GetMapping("/function")
    public List<Tree<String>> obtainAllFunctionsOfTheSmartWarehouse() {
        List<TreeNode> nodeList = CollUtil.newArrayList();
        int i=0;
        for (SmartWarehouseFunctionEnum value : SmartWarehouseFunctionEnum.values()) {
            
            
            //1.将数据库中的数据一次性查询出来封装到List中
            //2.在这里将从数据库查询的所有数据List中id,pid(父级id),name 依次次封装到longTreeNode中
           
            
            TreeNode<String> longTreeNode = new TreeNode<>(value.getId(), value.getPid(), value.getName(), 0);
            // 如果还需要给树形添加其他字段,返回给前端,需使用map进行封装
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("返回给前端的字段1", i++);// 返回给前端的字段:open_function
            hashMap.put("返回给前端的字段2",i+1);
            longTreeNode.setExtra(hashMap);
            nodeList.add(longTreeNode);
        }


        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都要默认值的
        //设置权重对应的名称
        treeNodeConfig.setWeightKey("order");
        //设置ID对应的名称
        treeNodeConfig.setIdKey("id");//如果返回给前端的id不叫id,可以修改这里的值
// 最大递归深度
        treeNodeConfig.setDeep(3);//这个是设置树形结构的层级

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        											     // 0表示最顶层的id是0,即最高的父级id为多少
        List<Tree<String>> build = TreeUtil.<TreeNode, String>build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId((String) treeNode.getId());
                    tree.setParentId((String) treeNode.getParentId());
                    tree.setName(treeNode.getName());

                    //这里的putAll与put的区别:put将之前需要给前端的其他字段封装成Map,进行多嵌套了一成
                    tree.putAll(treeNode.getExtra());
                    //tree.put("open_function",treeNode.getExtra());
                });

        return build;


    }


}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/188517.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!