当前位置: 首页 > 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 <脚本文件> 在…...

大数据学习栈记——Neo4j的安装与使用

本文介绍图数据库Neofj的安装与使用&#xff0c;操作系统&#xff1a;Ubuntu24.04&#xff0c;Neofj版本&#xff1a;2025.04.0。 Apt安装 Neofj可以进行官网安装&#xff1a;Neo4j Deployment Center - Graph Database & Analytics 我这里安装是添加软件源的方法 最新版…...

golang循环变量捕获问题​​

在 Go 语言中&#xff0c;当在循环中启动协程&#xff08;goroutine&#xff09;时&#xff0c;如果在协程闭包中直接引用循环变量&#xff0c;可能会遇到一个常见的陷阱 - ​​循环变量捕获问题​​。让我详细解释一下&#xff1a; 问题背景 看这个代码片段&#xff1a; fo…...

FFmpeg 低延迟同屏方案

引言 在实时互动需求激增的当下&#xff0c;无论是在线教育中的师生同屏演示、远程办公的屏幕共享协作&#xff0c;还是游戏直播的画面实时传输&#xff0c;低延迟同屏已成为保障用户体验的核心指标。FFmpeg 作为一款功能强大的多媒体框架&#xff0c;凭借其灵活的编解码、数据…...

无法与IP建立连接,未能下载VSCode服务器

如题&#xff0c;在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈&#xff0c;发现是VSCode版本自动更新惹的祸&#xff01;&#xff01;&#xff01; 在VSCode的帮助->关于这里发现前几天VSCode自动更新了&#xff0c;我的版本号变成了1.100.3 才导致了远程连接出…...

关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案

问题描述&#xff1a;iview使用table 中type: "index",分页之后 &#xff0c;索引还是从1开始&#xff0c;试过绑定后台返回数据的id, 这种方法可行&#xff0c;就是后台返回数据的每个页面id都不完全是按照从1开始的升序&#xff0c;因此百度了下&#xff0c;找到了…...

GitHub 趋势日报 (2025年06月08日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作

一、上下文切换 即使单核CPU也可以进行多线程执行代码&#xff0c;CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短&#xff0c;所以CPU会不断地切换线程执行&#xff0c;从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别 直接训练提示词嵌入向量的核心区别 您提到的代码: prompt_embedding = initial_embedding.clone().requires_grad_(True) optimizer = torch.optim.Adam([prompt_embedding...

智能分布式爬虫的数据处理流水线优化:基于深度强化学习的数据质量控制

在数字化浪潮席卷全球的今天&#xff0c;数据已成为企业和研究机构的核心资产。智能分布式爬虫作为高效的数据采集工具&#xff0c;在大规模数据获取中发挥着关键作用。然而&#xff0c;传统的数据处理流水线在面对复杂多变的网络环境和海量异构数据时&#xff0c;常出现数据质…...

docker 部署发现spring.profiles.active 问题

报错&#xff1a; org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...