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

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。collection、ofType、select的联合用法(Mybatis实现树状结构查询),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

需求

得到树结构数据也可以用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
        }
    ]
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/202786.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!