使用mybatis的resultMap进行复杂查询

本来写了一份,临时有事忘保存,丢了,现在重新写一份

使用mybatis的resultMap进行复杂查询

首先来了解一下数据表的结构。

 

 现在共有两张表,paper_template和paper_template_question_setting,其中paper_template是主表,setting是从表,主要负责记录模板中的一些设置,两表是一对多的关系

现在我想查询模板列表,并且附带模板的设置信息

select t.id,t.name,s.* from paper_template as t
left join paper_template_question_setting as s on t.id= s.template_id
where t.id=104

 对应的resultmap应该这么写

<resultMap id="testResultMap" type="com.xkw.its.homework.papertemplate.PaperTemplateDto">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="organization_id" jdbcType="VARCHAR" property="organizationId"/>
        <result column="stage_id" jdbcType="VARCHAR" property="stageId"/>
        <result column="subject_id" jdbcType="INTEGER" property="subjectId"/>
        <result column="grade_id" jdbcType="INTEGER" property="gradeId"/>
        <result column="semester" jdbcType="TINYINT" property="semester"/>
        <result column="year" jdbcType="INTEGER" property="year"/>
        <result column="create_user_id" jdbcType="VARCHAR" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_user_id" jdbcType="VARCHAR" property="updateUserId"/>
        <result column="explain_info" jdbcType="VARCHAR" property="explainInfo"/>
        <result column="difficulty_degree" jdbcType="VARCHAR" property="difficultyDegree"/>
        <result column="student_info" jdbcType="VARCHAR" property="studentInfo"/>
        <result column="sealing_line_text" jdbcType="VARCHAR" property="sealingLineText"/>
        <result column="is_deleted" jdbcType="BIT" property="isDeleted"/>
        <result column="source" jdbcType="TINYINT" property="source"/>
        <collection property="paperTemplateQuestionSettingDtoList"
                    ofType="com.xkw.its.homework.papertemplatequestionsetting.PaperTemplateQuestionSetting">
            <id column="id" jdbcType="INTEGER" property="id"/>
            <result column="start" jdbcType="INTEGER" property="start"/>
            <result column="end" jdbcType="INTEGER" property="end"/>
            <result column="question_count" jdbcType="INTEGER" property="questionCount" />
            <result column="is_composite" jdbcType="BIT" property="isComposite" />
            <result column="qcourse_id" jdbcType="INTEGER" property="courseId" />
            <result column="qsubject_id" jdbcType="INTEGER" property="subjectId"/>
            <result column="question_type_id" jdbcType="VARCHAR" property="questionTypeId"/>
            <result column="score" jdbcType="DOUBLE" property="score"/>
            <result column="template_id" jdbcType="INTEGER" property="templateId"/>
            <result column="difficulty_id" jdbcType="INTEGER" property="difficultyId" />
        </collection>
    </resultMap>

 

 此处做一个说明

property 对应的类中的字段名
collection 对应一个集合
ofType  是集合对应的类名
将子类的各个字段放在
collection 中即可

接口的查询结果如下图所示

 

 

  对比从数据库的直接查询结果,和从接口获取的结果发现:

paperTemplateQuestionSettingDtoList接口中只返回一条,并且id和template表中的id一样。这是因为本次查询中出现了两个id,查询映射自动默认第一个。解决方法就是在查询语句中为setting表中的id重命名,代码如下:
select *,q.id as q_id
    from paper_template t
    left join paper_template_catalog c on t.id=c.template_id
    left join paper_template_question_setting q on t.id=q.template_id
    where t.id=104

 

 

 今天只测试了带集合的用法,带对象的用法还没有测试,改天再来补充

 

posted @ 2019-11-26 18:20  快速突围  阅读(342)  评论(0编辑  收藏  举报