当前位置: 首页 > news >正文

多级目录SQL分层查询

需求:有多级目录,而目录的层级是不固定的,如下图所示:

 数据结构:

sql语句:

<select id="getList" resultType="com.hikvision.idatafusion.dhidata.bean.vo.knowledgebase.KnowledgeBaseTypeTreeVo">with recursive tree as(select distinct kbt.id, kbt.type_name, kbt.type_sort, kbt.parent_id, 1 as type_level,kbt.id::text as id_path, kbt.type_name::text as type_name_path, kbt.type_sort::text as type_sort_pathfrom knowledge_base_type kbt<if test="excludeNoDataType">join knowledge_base kb on kb.delete_flag = 0 and kb.type_id_list <![CDATA[&&]]> array[kbt.id]::int8[]and kb.publish_state = '1'</if>where kbt.delete_flag = 0unionselect tree.id, tree.type_name, tree.type_sort, parent.parent_id, tree.type_level + 1 as type_level,concat(parent.id, '/', tree.id_path) as id_path,concat(parent.type_name, '/', tree.type_name_path) as type_name_path,concat(parent.type_sort, '/', tree.type_sort_path) as type_sort_pathfrom treejoin knowledge_base_type parent on parent.delete_flag = 0 and parent.id = tree.parent_id)select id, type_name, type_sort, type_level, id_path, type_name_path, type_sort_pathfrom (select *,  ROW_NUMBER() OVER (PARTITION BY id ORDER BY type_level desc) AS rn from tree) twhere t.rn = 1
</select>

业务处理 :

/*** 是否排除没有数据的分类*/
@Override
public List<KnowledgeBaseTypeTreeVo> getList(boolean excludeNoDataType) {KnowledgeBaseTypeQueryDto knowledgeBaseTypeQueryDto = new KnowledgeBaseTypeQueryDto();knowledgeBaseTypeQueryDto.setExcludeNoDataType(excludeNoDataType);List<KnowledgeBaseTypeTreeVo> knowledgeBaseTypeTreeVoList  = knowledgeBaseTypeDao.getList(knowledgeBaseTypeQueryDto);// 对分类进行分层、排序Map<Long, KnowledgeBaseTypeTreeVo> firstLevelMap = new HashMap<>(); // 一级分类for (KnowledgeBaseTypeTreeVo knowledgeBaseType : knowledgeBaseTypeTreeVoList) {String[] idPath = knowledgeBaseType.getIdPath().split("/");// 生成一级分类long firstLevelId = Long.parseLong(idPath[0]);if(!firstLevelMap.containsKey(firstLevelId)) {KnowledgeBaseTypeTreeVo firstLevel = new KnowledgeBaseTypeTreeVo();firstLevel.setId(firstLevelId);firstLevel.setTypeName(knowledgeBaseType.getTypeNamePath().split("/")[0]);firstLevel.setTypeSort(Integer.parseInt(knowledgeBaseType.getTypeSortPath().split("/")[0]));firstLevel.setParentId(knowledgeBaseType.getParentId());firstLevelMap.put(firstLevelId, firstLevel);}// 遍历生成二级、三级...分类KnowledgeBaseTypeTreeVo firstLevel = firstLevelMap.get(firstLevelId); // 一级Map<Long, KnowledgeBaseTypeTreeVo> childrenMap = firstLevel.getChildrenMap();for (int i = 1; i < idPath.length; i++) { // 从二级开始遍历long typeId = Integer.parseInt(idPath[i]);if(!childrenMap.containsKey(typeId)) {KnowledgeBaseTypeTreeVo treeVo = new KnowledgeBaseTypeTreeVo();treeVo.setId(typeId);treeVo.setTypeName(knowledgeBaseType.getTypeNamePath().split("/")[i]);treeVo.setTypeSort(Integer.parseInt(knowledgeBaseType.getTypeSortPath().split("/")[i]));treeVo.setParentId((long) Integer.parseInt(idPath[i-1]));childrenMap.put(typeId, treeVo);}childrenMap = childrenMap.get(typeId).getChildrenMap(); // 下一级}// 排序for(KnowledgeBaseTypeTreeVo firstKnowledgeBaseType : firstLevelMap.values()) {this.sortChildren(firstKnowledgeBaseType);}knowledgeBaseType.setChildrenMap(firstLevelMap);}// 结果处理后返回List<KnowledgeBaseTypeTreeVo> knowledgeBaseTypeTreeList = firstLevelMap.values().stream().collect(Collectors.toList());return knowledgeBaseTypeTreeList;
}/*
* 分类按TypeSort倒序
* */
private void sortChildren(KnowledgeBaseTypeTreeVo treeVo) {if(MapUtils.isEmpty(treeVo.getChildrenMap())) {return;}treeVo.setChildren(treeVo.getChildrenMap().values().stream().sorted(Comparator.comparingInt(KnowledgeBaseTypeTreeVo::getTypeSort).reversed()).collect(Collectors.toList()));for(KnowledgeBaseTypeTreeVo child: treeVo.getChildrenMap().values()) {sortChildren(child);}
}

返回数据实体类:

@Data
@ToString
@EqualsAndHashCode(callSuper = false)
public class KnowledgeBaseTypeTreeVo implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "ID")private Long id;@ApiModelProperty(value = "类型名称")private String typeName;@ApiModelProperty(value = "序号,排序")private Integer typeSort;@ApiModelProperty(value = "父节点ID")public Long parentId;@ApiModelProperty(value = "分类层级")private Integer typeLevel;@ApiModelProperty(value = "分类ID层级路径")private String idPath;@ApiModelProperty(value = "分类名称层级路径")private String typeNamePath;@ApiModelProperty(value = "分类排序层级路径")private String typeSortPath;@ApiModelProperty(value = "子分类")private List<KnowledgeBaseTypeTreeVo> children = new ArrayList();@ApiModelProperty(value = "分类")private Map<Long, KnowledgeBaseTypeTreeVo> childrenMap = new HashMap<>();
}

相关文章:

多级目录SQL分层查询

需求&#xff1a;有多级目录&#xff0c;而目录的层级是不固定的&#xff0c;如下图所示&#xff1a; 数据结构&#xff1a; sql语句&#xff1a; <select id"getList" resultType"com.hikvision.idatafusion.dhidata.bean.vo.knowledgebase.KnowledgeBaseT…...

VulnHub-SickOs1.1靶机笔记

SickOs1.1靶机笔记 概述 Vulnhub的靶机sickos1.1 主要练习从互联网上搜取信息的能力&#xff0c;还考察了对代理使用&#xff0c;目录爆破的能力&#xff0c;很不错的靶机 靶机地址&#xff1a; 链接: https://pan.baidu.com/s/1JOTvKbfT-IpcgypcxaCEyQ?pwdytad 提取码: yt…...

【Python】数据可视化之点线图

目录 散点图 气泡图 时序图 关系图 ​​​​​​​ 散点图 Scatterplot&#xff08;散点图&#xff09;是一种用于展示两个变量之间关系的图表类型。在散点图中&#xff0c;每个观测值&#xff08;或数据点&#xff09;都被表示为一个点&#xff0c;其中横轴&#xff08;…...

jupyter使用pytorch

1、激活环境 以下所有命令都在Anaconda Prompt中操作。 conda activate 环境名称我的环境名称是myenv 如果不知道自己的pytorch配在哪个环境&#xff0c;就用下面方法挨个试。 2、安装jupyter 1、安装 pip install jupyter2、如果已经安装&#xff0c;检查jupyter是否已…...

Electron 安装以及搭建一个工程

安装Node.js 在使用Electron进行开发之前&#xff0c;需要安装 Node.js。 官方建议使用最新的LTS版本。 检查 Node.js 是否正确安装&#xff1a; # 查看node版本 node -v # 查看npm版本 npm -v注意 开发者需要在开发环境安装 Node.js 才能编写 Electron 项目&#xff0c;但是…...

羽毛类型检测系统源码分享

羽毛类型检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vis…...

Xiaojie雷达之路---doa估计(dbf、capon、music算法)

Hello,大家好,我是Xiaojie,欢迎大家能够和Xiaojie来一起学习毫米波雷达知识,本篇文章主要是介绍一下雷达信号处理中的dbf、capon、music测角算法,一起来看看吧!!! 前言 本文从信号模型、dbf原理、capon原理、music原理以及代码仿真进行展开描述。 信号模型 阵列接收到…...

十大排序算法总结

完整文档见 排序算法总结——语雀文档 比较类排序&#xff1a;通过比较来决定元素间的相对次序&#xff0c;由于其时间复杂度不能突破O(nlogn)&#xff0c;因此也称为非线性时间比较类排序。 非比较类排序&#xff1a;不通过比较来决定元素间的相对次序&#xff0c;它可以突破…...

大厂AI必备数据结构与算法——链表(三)详细文档

冲冲冲&#xff01;开干 神马&#xff01;神马&#xff01;神马&#xff0c;一向让我们学习起来抓耳挠腮的数据结构课程竟然也有教程&#xff1f;还那么详细&#xff1f;&#xff1f;真的假的&#xff1f; 那么好&#xff0c;胡广告诉你是假的&#xff0c;哈哈哈哈哈哈哈哈哈…...

一键自动化配置OpenHarmony编译环境

一、概述 本工程旨在对Ubuntu一键初始化配置环境&#xff0c;解决OpenHarmony的编译依赖问题&#xff0c;基于本脚本配置后配合一键下载OpenHarmony代码便能轻松掌控OpenHarmony的下载、编译。 当前建议使用稳定分支Itopen-2.0-Release&#xff0c;该分支是经过多次测试OK的&…...

不同领域的常见 OOD(Out-of-Distribution)数据集例子

以下是几个来自不同领域的常见 OOD&#xff08;Out-of-Distribution&#xff09;数据集例子&#xff0c;这些数据集常用于测试和研究模型在分布变化或分布外数据上的泛化能力&#xff1a; 1. 计算机视觉领域 CIFAR-10 vs. CIFAR-10-C / CIFAR-100-C: 描述&#xff1a;CIFAR-10…...

gRPC协议简介

gRPC 是谷歌开源的一套 RPC 协议框架。主要做两件事情&#xff1a;一是数据编码&#xff0c;二是请求映射。 数据编码 数据编码顾名思义就是在将请求的内存对像转化成可以传输的字节流发给服务端&#xff0c;并将收到的字节流再转化成内存对像。方法有很多&#xff0c;常见的…...

[dp+dfs]砝码称重

题目描述 现有 n n n 个砝码&#xff0c;重量分别为 a 1 , a 2 , … , a n a_1, a_2, \ldots,a_n a1​,a2​,…,an​ &#xff0c;在去掉 m m m 个砝码后&#xff0c;问最多能称量出多少不同的重量&#xff08;不包括 0 0 0 &#xff09;。 输入格式 第一行为有两个整数…...

MYSQL-查看表中字段属性语法(三)

查看表中字段全部信息 show full columns from database_name.table_name; show full columns from table_name;示例 mysql> show full columns from world.city; ----------------------------------------------------------------------------------------------------…...

第三讲 part 3:前端处理LINK3D - 代码解析 - 从main出发看总体流程(ROS1改为ROS2)

目录 1. ROS1 ->ROS21.1 包含头文件1.2 全局变量定义1.3 结构体定义1.4 点云容器定义1.5 图像处理相关变量1.6 ROS2发布者和订阅者定义1.7 全局变量,被不断更新1.8 点云处理相关变量1.9 图像描述符1.10 主函数1.10.1. 初始化ROS21.10.2. 创建节点1.10.3. 声明参数1.10.4. 设…...

移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——15.红黑树

1.红黑树的概念 红黑树&#xff0c;是一种二叉搜索树&#xff0c;但在每个结点上增加一个存储位表示结点的颜色&#xff0c;可以是Red或 Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制&#xff0c;红黑树确保没有一条路 径会比其他路径长出俩倍&#xff0c;…...

【C++】Eclipse技巧汇总

Eclipse C/C调试无法输入 在debug C/C程序时&#xff0c;Eclipse自带的窗口&#xff0c;无法读取cin等输入 解决办法&#xff1a; 参考&#xff1a;https://blog.csdn.net/sagjhdj/article/details/123271383 思路是调用外部console&#xff1a; 依次点击Debug>Debug Conf…...

Golang | Leetcode Golang题解之第430题扁平化多级双向链表

题目&#xff1a; 题解&#xff1a; func dfs(node *Node) (last *Node) {cur : nodefor cur ! nil {next : cur.Next// 如果有子节点&#xff0c;那么首先处理子节点if cur.Child ! nil {childLast : dfs(cur.Child)next cur.Next// 将 node 与 child 相连cur.Next cur.Chi…...

Java实现找色和找图功能

某天&#xff0c;张三接到一个任务需求&#xff0c;将一个Excel表格里面的员工信息&#xff0c;录入到员工系统里面&#xff0c;由于数据量非常大&#xff0c;操作起来巨慢。经过一段时间的操作和观察&#xff0c;他发现这种操作&#xff0c;非常有规律&#xff0c;基本就是一些…...

linux脚本工具

目录 shell工具查看Nvidia GPU状态查看某个监听端口是否存在设置局部代理查找关键字相关进程根据日常所需&#xff0c;持续更新 shell工具 减少重复性工作&#xff0c;简化工作流程&#xff0c;提高工作效率 将所编写的shell脚本赋予可执行权限 chmod x <脚本文件> 在…...

常见电机分类

文章目录电机分类电机分类 序号分类优点缺点驱动方式举例1直流电机结构简单、成本低、启动扭矩大、控制方便有电刷磨损&#xff0c;产生火花和噪音&#xff0c;寿命较短&#xff0c;高速下维护成本高PWM调速、H桥驱动(正/反转)玩具车、电动工具、风扇2步进精确的位置控制能力&…...

10个企业级Windows自动化场景:pywinauto终极应用指南

10个企业级Windows自动化场景&#xff1a;pywinauto终极应用指南 【免费下载链接】pywinauto pywinauto/pywinauto: 一个 Python 库&#xff0c;用于自动化 Windows 应用程序。特点是提供了丰富的函数和类库&#xff0c;可以用于控制鼠标、键盘和菜单等元素&#xff0c;实现自动…...

从仿真到真机:基于ROS2 Control和MoveIt2的Panda机械臂运动控制实战(Humble环境)

从仿真到真机&#xff1a;基于ROS2 Control和MoveIt2的Panda机械臂运动控制实战&#xff08;Humble环境&#xff09; 在工业自动化和科研领域&#xff0c;机械臂的运动控制正经历着从传统专用控制器向开源软件栈的转型。ROS2生态系统中的两大支柱——ROS2 Control和MoveIt2&…...

拆解 OpenHands(11)--- Runtime主要组件

本篇继续对 runtime 的解读&#xff0c;主要介绍 插件、执行系统和环境这三个组件。因为本系列借鉴的文章过多&#xff0c;可能在参考文献中有遗漏的文章&#xff0c;如果有&#xff0c;还请大家指出。0x01 三大组件本篇要介绍的几个组件如下&#xff1a;ActionExecutor&#x…...

5分钟掌握防撤回神器:让重要消息无处可逃

5分钟掌握防撤回神器&#xff1a;让重要消息无处可逃 【免费下载链接】RevokeMsgPatcher :trollface: A hex editor for WeChat/QQ/TIM - PC版微信/QQ/TIM防撤回补丁&#xff08;我已经看到了&#xff0c;撤回也没用了&#xff09; 项目地址: https://gitcode.com/GitHub_Tre…...

如何永久保存微信聊天记录?WeChatMsg免费工具终极指南

如何永久保存微信聊天记录&#xff1f;WeChatMsg免费工具终极指南 【免费下载链接】WeChatMsg 提取微信聊天记录&#xff0c;将其导出成HTML、Word、CSV文档永久保存&#xff0c;对聊天记录进行分析生成年度聊天报告 项目地址: https://gitcode.com/GitHub_Trending/we/WeCha…...

告别硬编码!Activiti7流程变量与监听器实战:动态分配审批人与业务数据流转

Activiti7流程变量与监听器实战&#xff1a;动态审批人分配与业务数据流转 在业务流程管理&#xff08;BPM&#xff09;领域&#xff0c;硬编码审批人始终是系统灵活性的主要障碍。当组织架构调整或审批规则变化时&#xff0c;传统方案往往需要重新部署流程定义。本文将深入探…...

BilibiliCommentScraper:革新性全量数据采集的技术突破方案

BilibiliCommentScraper&#xff1a;革新性全量数据采集的技术突破方案 【免费下载链接】BilibiliCommentScraper 项目地址: https://gitcode.com/gh_mirrors/bi/BilibiliCommentScraper 在当今数据驱动决策的时代&#xff0c;高效采集方案与完整数据获取已成为内容分析…...

SteamShutdown终极指南:让Steam下载完成后自动关机的完整解决方案

SteamShutdown终极指南&#xff1a;让Steam下载完成后自动关机的完整解决方案 【免费下载链接】SteamShutdown Automatic shutdown after Steam download(s) has finished. 项目地址: https://gitcode.com/gh_mirrors/st/SteamShutdown 还在为Steam大型游戏下载而熬夜等…...

MozJPEG色彩空间扩展终极指南:支持RGBX、BGRX等32位格式的完整教程

MozJPEG色彩空间扩展终极指南&#xff1a;支持RGBX、BGRX等32位格式的完整教程 【免费下载链接】mozjpeg Improved JPEG encoder. 项目地址: https://gitcode.com/gh_mirrors/mo/mozjpeg MozJPEG作为libjpeg-turbo的增强版本&#xff0c;不仅提供了卓越的JPEG压缩性能&a…...