当前位置: 首页 > 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}]
}
http://www.lryc.cn/news/291688.html

相关文章:

  • FLUENT Meshing Watertight Geometry工作流入门 - 4 局部加密区域
  • 前端添加富文本/Web 富文本编辑器wangeditor
  • 软件价值2-贪吃蛇游戏
  • 应用案例 | 基于三维机器视觉的汽车副车架在线测量解决方案
  • 线程的创建和使用threading.Thread()
  • 大数据学习之Redis,十大数据类型的具体应用(四)
  • 哪个牌子的头戴式耳机好?推荐性价比高的头戴式耳机品牌
  • Java EE 5 SDK架构
  • nop-entropy可逆计算入门(1)
  • C++(9) 虚函数
  • uniapp 使用canvas 画海报,有手粘贴即可用(拆成组件了,看后面)
  • Amazon Bedrock 的微调和持续预训练功能允许用户使用私有数据定制模型
  • Pyecharts绘制多种炫酷气泡图
  • C# 多线程(2)——线程同步
  • Java设计模式【工厂模式】
  • AI智能分析+明厨亮灶智慧管理平台助力“舌尖上的安全”
  • 【现代密码学基础】详解完美安全与香农定理
  • Python 将文本转换成语音播放 pyttsx3
  • FPGA高端项目:Xilinx Artix7系列FPGA 多路视频缩放拼接 工程解决方案 提供4套工程源码+技术支持
  • 开源模型应用落地-业务优化篇(三)
  • 基于SpringBoot+Vue实现的物流快递仓库管理系统
  • 编程笔记 html5cssjs 072 JavaScrip BigInt数据类型
  • matlab simulink 步进电机控制
  • 使用阿里云的IDaaS实现知行之桥EDI系统的单点登录
  • 基于微服务的高考志愿智能辅助决策系统(附源码)
  • LeetCode —— 137. 只出现一次的数字 II
  • pnpm、npm、yarn 包管理工具
  • 微服务知识
  • 如何在微信搭建私域流量池?
  • MySQL原理(三)锁定机制(1)综述