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

collection、ofType、select的联合用法(Mybatis实现树状结构查询)

需求

得到树结构数据也可以用lambda表达式也行,也可以直接循环递归也行,本文采用的是直接在Mybatis层得到结果,各有各的优势。

代码

1、实体类

@Data
public class CourseChapterVO implements Serializable {private static final long serialVersionUID = -67718827554034368L;/*** 主键ID*/private Integer id;/*** 课程ID*/private Integer courseId;/*** 课程章节名称*/private String chapterName;/*** 父ID*/private Integer chapterParent;/*** 层级*/private Integer chapterLevel;@JsonInclude(JsonInclude.Include.NON_EMPTY)  //当children为空时不返回    private List<CourseChapterVO> children;}

2、mapper.xml

<resultMap type="com.ruoyi.manager.vo.CourseChapterVO" id="CourseChapterMap2"><result property="id" column="id" jdbcType="INTEGER"/><result property="courseId" column="course_id" jdbcType="INTEGER"/><result property="chapterName" column="chapter_name" jdbcType="VARCHAR"/><result property="chapterParent" column="chapter_parent" jdbcType="INTEGER"/><result property="chapterLevel" column="chapter_level" jdbcType="INTEGER"/><collection property="children" ofType="com.ruoyi.manager.vo.CourseChapterVO" column="id" select="selectListTree2"/></resultMap><select id="selectListTree1" resultMap="CourseChapterMap2">select id , course_id, chapter_name, chapter_parent, chapter_levelfrom course_chapter where chapter_parent =0 </select><select id="selectListTree2" resultMap="CourseChapterMap2">select id , course_id, chapter_name, chapter_parent, chapter_level from course_chapter where chapter_parent =#{id}</select>

3、mapper.java

List<CourseChapterVO> selectListTree1(CourseChapter courseChapter);

4、serviceImpl.java

 @Overridepublic List<CourseChapterVO> queryAll(CourseChapter courseChapter) {return this.courseChapterMapper.selectListTree1(courseChapter);}

5、service.java

List<CourseChapterVO> queryAll(CourseChapter courseChapter);

6、controller.java

 @GetMapping("list")public R queryAll(CourseChapter courseChapter) {return R.data(this.courseChapterService.queryAll(courseChapter));}

7、实现效果

{"msg": "","code": 0,"data": [{"id": 1,"courseId": 5,"chapterName": "第一章节学习","chapterParent": 0,"chapterLevel": 1,"children": [{"id": 2,"courseId": 5,"chapterName": "第一章节学习子1","chapterParent": 1,"chapterLevel": 2},{"id": 3,"courseId": 5,"chapterName": "第一章节学习子2","chapterParent": 1,"chapterLevel": 2},{"id": 4,"courseId": 5,"chapterName": "第一章节学习子3","chapterParent": 1,"chapterLevel": 2}]},{"id": 5,"courseId": 5,"chapterName": "第二章节学习","chapterParent": 0,"chapterLevel": 1,"children": [{"id": 6,"courseId": 5,"chapterName": "第二章节学习子","chapterParent": 5,"chapterLevel": 2,"children": [{"id": 9,"courseId": 5,"chapterName": "第二章节学习子1子1","chapterParent": 6,"chapterLevel": 3}]},{"id": 7,"courseId": 5,"chapterName": "第二章节学习子1","chapterParent": 5,"chapterLevel": 2},{"id": 8,"courseId": 5,"chapterName": "第二章节学习子2","chapterParent": 5,"chapterLevel": 2}]},{"id": 10,"courseId": 5,"chapterName": "第三章节学习gfdgdf","chapterParent": 0,"chapterLevel": 1}]
}

相关文章:

  • 【极数系列】Flink环境搭建Linux版本 (03)
  • linux 云主机扩展磁盘的步骤
  • win11 蓝屏分析,没有一定的原因,没有解决办法,只能不断尝试
  • 常用命令-
  • 内置函数和推导式
  • 83.网游逆向分析与插件开发-背包的获取-自动化助手显示装备数据
  • FPGA硬件架构
  • 第26讲:顺序表的应用(通讯录)
  • 人工智能与机器学习——开启智能时代的里程碑
  • JavaEE-自定义SSM-编写核心- my spring bean工厂(IoC、DI)
  • MySQL库表操作 作业
  • C语言基础13
  • HarmonyOS 鸿蒙应用开发 (七、HTTP网络组件 axios 介绍及封装使用)
  • TortoiseSVN各版本汉化包下载
  • 学习使用Flask模拟接口进行测试
  • 网络传输文件的问题
  • JavaScript-如何实现克隆(clone)函数
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • css系列之关于字体的事
  • Git 使用集
  • Js基础——数据类型之Null和Undefined
  • js写一个简单的选项卡
  • JWT究竟是什么呢?
  • laravel with 查询列表限制条数
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • linux学习笔记
  • MySQL QA
  • Python利用正则抓取网页内容保存到本地
  • React-redux的原理以及使用
  • Redis的resp协议
  • Redis字符串类型内部编码剖析
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 测试如何在敏捷团队中工作?
  • 力扣(LeetCode)357
  • 通过npm或yarn自动生成vue组件
  • 温故知新之javascript面向对象
  • 一文看透浏览器架构
  • 在Unity中实现一个简单的消息管理器
  • 怎么将电脑中的声音录制成WAV格式
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • #大学#套接字
  • #每日一题合集#牛客JZ23-JZ33
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • $.ajax()方法详解
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (39)STM32——FLASH闪存
  • (9)STL算法之逆转旋转
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (八)Flask之app.route装饰器函数的参数
  • (第一天)包装对象、作用域、创建对象
  • (图)IntelliTrace Tools 跟踪云端程序