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_level
        from 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

 @Override
    public 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
        }
    ]
}

相关推荐

  1. postgresql树状结构查询示例

    2024-01-28 12:08:01       41 阅读
  2. MyBatis可以如何实现分页查询

    2024-01-28 12:08:01       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-28 12:08:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-28 12:08:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-28 12:08:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-28 12:08:01       20 阅读

热门阅读

  1. Python在网络安全防御中的应用与实践

    2024-01-28 12:08:01       35 阅读
  2. @Scheduled笔记240124

    2024-01-28 12:08:01       31 阅读
  3. vue el-date-picker 开始日期不能大于结束日期

    2024-01-28 12:08:01       35 阅读
  4. C 练习实例49-宏#define命令练习3

    2024-01-28 12:08:01       35 阅读
  5. Git(7)之提交消息模板

    2024-01-28 12:08:01       31 阅读
  6. MongoDB基本使用

    2024-01-28 12:08:01       32 阅读
  7. leetcode 字符串相关题目

    2024-01-28 12:08:01       32 阅读
  8. 11.2 Web开发_CSS入门(❤❤)

    2024-01-28 12:08:01       38 阅读
  9. 五、详细设计说明书(软件工程)

    2024-01-28 12:08:01       34 阅读
  10. 从二叉树角度来理解快速排序

    2024-01-28 12:08:01       30 阅读
  11. 【WebGPU】简介和开始

    2024-01-28 12:08:01       31 阅读
  12. 深入了解 Spring BeanPostProcessor 的应用

    2024-01-28 12:08:01       34 阅读
  13. 使用virtualenv管理python环境

    2024-01-28 12:08:01       33 阅读