简单记录一下在java的Mybatis-plus中用一个SQL语句查询一个嵌套的实体类(实体类中有List,List中还有List)

简单记录一下在java的Mybatis-plus中用一个SQL语句查询一个嵌套的实体类(实体类中有List,List中还有List)

也就是KpCourseInfoClientDetailVO课程详情类中含有List courseChapterList章节列表,然后章节列表中含有List lessonList课时列表,通过一个SQL查询得到结果

KpCourseInfoClientDetailVO类

java 复制代码
@Data
public class KpCourseInfoClientDetailVO extends BaseEntity {

    /**
     * 主键ID
     */
    @ApiModelProperty(value = "主键ID",example = "1" , position = 1)
    private Long id;

    /**
     * 图片
     */
    @ApiModelProperty(value = "图片",example = "https://yq-0.obs.cn-east-3.myhuaweicloud.com/12351.bjzgh.org/image/20220824/1661320343049.png" , position = 5)
    private String pic;

    /**
     * 名称
     */
    @ApiModelProperty(value = "名称",example = "Java修仙手册" , position = 11)
    private String title;

    /**
     * 课时数量
     */
    @ApiModelProperty(value = "课时数量",example = "5" , position = 21)
    private Integer lessons;


    /**
     * 最近在学人数
     */
    @ApiModelProperty(value = "最近在学人数",example = "5" , position = 31)
    private Integer clicks;

    /**
     * 总学时
     */
    @ApiModelProperty(value = "总学时(秒)",example = "120" , position = 35)
    private Integer duration;

    /**
     * 已学时长
     */
    @ApiModelProperty(value = "已学时长(秒)",example = "60" , position = 37)
    private Integer studyDuration;

    /**
     * 章节列表
     */
    @ApiModelProperty(value = "章节列表", position = 71)
    private List<KpCourseChapterClientVO> courseChapterList;

}

KpCourseChapterClientVO类

java 复制代码
@Data
public class KpCourseChapterClientVO extends BaseEntity {

    /**
     * 主键ID
     */
    @ApiModelProperty(value = "主键ID",example = "1" , position = 1)
    private Long id;

    /**
     * 名称
     */
    @ApiModelProperty(value = "名称",example = "优秀奖《老年人便秘护理健康宣教》" , position = 51)
    private String title;

    /**
     * 课时列表
     */
    @ApiModelProperty(value = "课时列表", position = 71)
    private List<KpLessonClientListVO> lessonList;


}

KpLessonClientListVO类

java 复制代码
@Data
public class KpLessonClientListVO extends BaseEntity {

    /**
     * 主键ID
     */
    @ApiModelProperty(value = "主键ID",example = "1" , position = 1)
    private Long id;

    /**
     * 名称
     */
    @ApiModelProperty(value = "名称",example = "走进故宫------下雪天的故宫是什么样的" , position = 31)
    private String title;

    /**
     * 视频时长,单位:秒
     */
    @ApiModelProperty(value = "视频时长,单位:秒",example = "120" , position = 111)
    private Double duration;

}

首先在Navicat中验证SQL的准确性

sql 复制代码
#查看前端课程详情
SELECT
	kci.id,
	kci.pic,
	kci.title,
	kci.lessons,
	kci.clicks,
	kci.duration,
	kcc.id chapterId,
	kcc.title chapterName,
	kl.id lessonId,
	kl.title lessonName,
	kl.duration lessonDuration 
FROM
	kp_course_info kci
	LEFT JOIN kp_course_chapter kcc ON kcc.course_id = kci.id
	LEFT JOIN kp_lesson kl ON kl.chapter_id = kcc.id 
WHERE
	kci.id = 11567 
	AND kci.data_status = 0 
	AND kcc.data_status = 0 
	AND kl.data_status = 0 
	AND kl.available_flag = 1 
	AND kl.review_status = 5
ORDER BY kcc.id DESC

得到下列结果,说明SQL没有问题

然后在KpCourseInfoMapper.xml中实现

xml 复制代码
<resultMap id="getCourseInfoDetail" type="com.chengyun.beijing.forms.exam.domain.vo.KpCourseInfoClientDetailVO">
    <id property="id" column="id"></id>
    <result property="pic" column="pic"></result>
    <result property="title" column="title"></result>
    <result property="lessons" column="lessons"></result>
    <result property="clicks" column="clicks"></result>
    <result property="duration" column="duration"></result>
    <collection property="courseChapterList" ofType="com.chengyun.beijing.forms.exam.domain.vo.KpCourseChapterClientVO" column="chapterId">
        <result property="id" column="chapterId"></result>
        <result property="title" column="chapterName"></result>
        <association property="lessonList" javaType="java.util.List" resultMap="lessonMap"/>
    </collection>
</resultMap>

<resultMap id="lessonMap" type="com.chengyun.beijing.forms.exam.domain.vo.KpLessonClientListVO">
    <result property="id" column="lessonId"></result>
    <result property="title" column="lessonName"></result>
    <result property="duration" column="lessonDuration"></result>
</resultMap>

<select id="getCourseInfoDetail" resultMap="getCourseInfoDetail">
    SELECT
        kci.id,
        kci.pic,
        kci.title,
        kci.lessons,
        kci.clicks,
        kci.duration,
        kcc.id chapterId,
        kcc.title chapterName,
        kl.id lessonId,
        kl.title lessonName,
        kl.duration lessonDuration
    FROM
        kp_course_info kci
            LEFT JOIN kp_course_chapter kcc ON kcc.course_id = kci.id
            LEFT JOIN kp_lesson kl ON kl.chapter_id = kcc.id
    ${ew.customSqlSegment}
</select>

因为是在实体类中List嵌套List,所以外部的List直接用collection 标签来写,然后再给其内部嵌套的List标签写个resultMap 来对应参数,最后通过接口调用就能拿到想要的参数结构了

相关推荐
小张认为的测试1 分钟前
Liunx上Jenkins 持续集成 Java + Maven + TestNG + Allure + Rest-Assured 接口自动化项目
java·ci/cd·jenkins·maven·接口·testng
蘑菇丁31 分钟前
ansible批量生产kerberos票据,并批量分发到所有其他主机脚本
java·ide·eclipse
呼啦啦啦啦啦啦啦啦1 小时前
【Redis】持久化机制
java·redis·mybatis
我想学LINUX2 小时前
【2024年华为OD机试】 (A卷,100分)- 微服务的集成测试(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·微服务·集成测试
Yeats_Liao4 小时前
Navicat 导出表结构后运行查询失败ERROR 1064 (42000): You have an error in your SQL syntax;
数据库·sql
空の鱼7 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
P7进阶路8 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花9 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb9 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od