首页 > 其他分享 >hutool 使用 TreeUtil 查询树型结构

hutool 使用 TreeUtil 查询树型结构

时间:2023-11-24 11:25:07浏览次数:40  
标签:sort hutool displayStatus typeName contactWay TreeUtil parentId 树型 remarkTypeId

之前写过一篇用stream流实现查询树型结构的文章,现在以hutool中的TreeUtil再来实现一次,之前的帖子Java Stream流实现递归查询树型结构

查询出所有数据,用父节点递归查询出所有子节点数据

   /**
     * 封装备注分类集合
     *
     * @param remarkTypeList 备注分类集合
     * @return 递归好的集合
     */
   @Override
    public List<Tree<String>> queryTreeList(List<RemarkType> remarkTypeList) {
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("remarkTypeId");
        treeNodeConfig.setNameKey("typeName");
        treeNodeConfig.setParentIdKey("parentId");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器
        return TreeUtil.build(remarkTypeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getRemarkTypeId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setName(treeNode.getTypeName());
                    tree.putExtra("displayStatus",treeNode.getDisplayStatus());
                    tree.putExtra("sort",treeNode.getSort());
                    tree.putExtra("contactWay",treeNode.getContactWay());
                });
    }

查询结果:

{
    "msg": "操作成功",
    "code": 200,
    "data": [
        {
            "remarkTypeId": "1",
            "parentId": "0",
            "typeName": "未接通",
            "displayStatus": "0",
            "sort": 0,
            "contactWay": "电话",
            "children": [
                {
                    "remarkTypeId": "4",
                    "parentId": "1",
                    "typeName": "关机",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                },
                {
                    "remarkTypeId": "5",
                    "parentId": "1",
                    "typeName": "空号",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                },
                {
                    "remarkTypeId": "6",
                    "parentId": "1",
                    "typeName": "停机",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                },
                {
                    "remarkTypeId": "7",
                    "parentId": "1",
                    "typeName": "通话中",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                },
                {
                    "remarkTypeId": "8",
                    "parentId": "1",
                    "typeName": "无法接通",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                },
                {
                    "remarkTypeId": "9",
                    "parentId": "1",
                    "typeName": "无人接听",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                }
            ]
        },
        {
            "remarkTypeId": "2",
            "parentId": "0",
            "typeName": "已接通",
            "displayStatus": "0",
            "sort": 0,
            "contactWay": "电话",
            "children": [
                {
                    "remarkTypeId": "10",
                    "parentId": "2",
                    "typeName": "开场白挂机",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "22",
                            "parentId": "10",
                            "typeName": "用户不说话",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "21",
                            "parentId": "10",
                            "typeName": "开场白挂断",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "11",
                    "parentId": "2",
                    "typeName": "客户忙",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "23",
                            "parentId": "11",
                            "typeName": "在忙没空\r\n",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "12",
                    "parentId": "2",
                    "typeName": "需要屏蔽",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "24",
                            "parentId": "12",
                            "typeName": "说不要再打了\r\n",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "13",
                    "parentId": "2",
                    "typeName": "已充值",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "25",
                            "parentId": "13",
                            "typeName": "套餐转移",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "26",
                            "parentId": "13",
                            "typeName": "正在使用",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "14",
                    "parentId": "2",
                    "typeName": "已退货",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                },
                {
                    "remarkTypeId": "15",
                    "parentId": "2",
                    "typeName": "有充值意向",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "27",
                            "parentId": "15",
                            "typeName": "用户承诺近期充值",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "28",
                            "parentId": "15",
                            "typeName": "已引导",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "16",
                    "parentId": "2",
                    "typeName": "物流正在路上",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "29",
                            "parentId": "16",
                            "typeName": "还没拿到已引导\r\n",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "17",
                    "parentId": "2",
                    "typeName": "用户不需要",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "30",
                            "parentId": "17",
                            "typeName": "骂人",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "31",
                            "parentId": "17",
                            "typeName": "准备退货",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "32",
                            "parentId": "17",
                            "typeName": "试用不满意",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "33",
                            "parentId": "17",
                            "typeName": "价格高",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "34",
                            "parentId": "17",
                            "typeName": "说我们骗人",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "35",
                            "parentId": "17",
                            "typeName": "在用其他家的",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "36",
                            "parentId": "17",
                            "typeName": "设备不在身边",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "37",
                            "parentId": "17",
                            "typeName": "不用了不需要",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "18",
                    "parentId": "2",
                    "typeName": "用户不知情购买",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "38",
                            "parentId": "18",
                            "typeName": "非本人购买,已让转告",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "39",
                            "parentId": "18",
                            "typeName": "用户说没购买",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                },
                {
                    "remarkTypeId": "19",
                    "parentId": "2",
                    "typeName": "用户犹豫\r\n\r\n",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "40",
                            "parentId": "19",
                            "typeName": "赠送免费流量",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "41",
                            "parentId": "19",
                            "typeName": "答应减免充值金额",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                }
            ]
        },
        {
            "remarkTypeId": "3",
            "parentId": "0",
            "typeName": "其他",
            "displayStatus": "0",
            "sort": 0,
            "contactWay": "电话",
            "children": [
                {
                    "remarkTypeId": "44",
                    "parentId": "3",
                    "typeName": "电商备注",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话"
                },
                {
                    "remarkTypeId": "20",
                    "parentId": "3",
                    "typeName": "一退",
                    "displayStatus": "0",
                    "sort": 0,
                    "contactWay": "电话",
                    "children": [
                        {
                            "remarkTypeId": "42",
                            "parentId": "20",
                            "typeName": "物流拦截",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        },
                        {
                            "remarkTypeId": "43",
                            "parentId": "20",
                            "typeName": "中途退货",
                            "displayStatus": "0",
                            "sort": 0,
                            "contactWay": "电话",
                            "children": null
                        }
                    ]
                }
            ]
        }
    ]
}

根据子节点查询所有父节点数据

标签:sort,hutool,displayStatus,typeName,contactWay,TreeUtil,parentId,树型,remarkTypeId
From: https://www.cnblogs.com/Linzj5950/p/17853324.html

相关文章

  • 使用 hutool 工具类生成和识别二维码图片
    实际工作中生成二维码,通过手机进行扫码查看的需求比较多。Hutool工具包中有很多实用的工具类,其中就包含二维码生成和识别的QrCodeUtil工具类,默认生成白底黑色的二维码,也可以自定义二维码的样式,比如设置背景色、前景色、设置边距以及在二维码中心显示自定义的logo等等,使用起来......
  • 使用hutool工具包对集合中的数据组装成树形的结构
    //配置Listlist=newArrayList<>();TreeNodeConfigtreeNodeConfig=newTreeNodeConfig();//指定节点字段的名称和父级节点的字段名称treeNodeConfig.setIdKey("id");treeNodeConfig.setParentIdKey("pid");//最大递归深度treeNodeConfig.setDeep(3);//对集合中的......
  • 不依赖于cn.hutool.extra.servlet.ServletUtil,使用Spring其他方式实现文件下载的功能
     在JDK17下,如下代码引用了cn.hutool.extra.servlet包下的ServletUtil工具类的ServletUtil.write方法,执行时报”Cannotresolvemethod'write(jakarta.servlet.http.HttpServletResponse,java.io.File)'“BUG,download方法通过cn.hutool.extra.servlet.ServletUtil的方法来实现......
  • Hutool日志组件配合testng+slf4j的简单使用
    Hutool是一个Java工具包类库,对文件、流、加密解密、转码、正则、线程、XML等JDK方法进行封装,组成各种Util工具类,地址是https://hutool.cn/具体的用法和api文档在页面上有,就不赘述了,这里只分享在使用testng做单元测试时Hutool-log的简单使用,文档上写得比较简陋,也不太好理解首先是......
  • JavaScript树型数据与一维数组相互转换方式
     /***@description一维数组转树形数据**/exportconstarrToTree=(data=[],conf={})=>(((data,{id='id',parentId='parentId',children='children'})=>{letresult=[]if(!Array.isArray(data)){r......
  • HuTool JAVA 工具类神器
    版权声明:本文为CSDN博主「有多勉为其难」的原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/abst122/article/details/124091375Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有......
  • Hutool:一行代码搞定数据脱敏
    1.什么是数据脱敏1.1数据脱敏的定义数据脱敏:指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护。这样就可以在开发、测试和其它非生产环境以及外包环境中安全地使用脱敏后的真实数据集。在涉及客户安全数据或者一些商业性敏感数据的情况下,在不违反系统规则......
  • 利用hutool中的 ExcelWriter 编写导出信息
    我们现在很多网站都有导出信息这个功能,而今天我介绍的是hutool中的ExcelWriter,通过这个在后端编写代码,将信息导出在excel,然后通过浏览器进行保存下载。后端代码:/***导出接口*/@GetMapping("/export")publicvoidexport(HttpServletResponseresponse......
  • hutool
    Hutool(也称为Hu-tool或HuTool)是一个Java编程语言的工具库,旨在简化和提高Java开发过程中的日常任务和常见操作。Hutool库提供了许多实用工具、工具类和函数,用于处理字符串、日期、文件、加密、网络、XML、JSON、集合操作、图片处理、等等。它被设计为易于使用,而且功能丰富。以下是......
  • Java Hutool递归解压文件
    importcn.hutool.core.io.FileUtil;importcn.hutool.core.util.ZipUtil;importjava.io.File;importjava.nio.charset.Charset;publicclassRecursiveUnzip{finalstaticFiledestDir=newFile("D:\\python\\newProject\\excel");pub......