首页 > 代码库 > Mybatis 级联查询 (一对多 )

Mybatis 级联查询 (一对多 )

后台系统中 涉及到添加试卷 问题 答案的一个模块的。我需要通过试卷 查询出所有的试题,以及试题的答案。这个主要要使用到Mybatis的级联查询。

通过试卷 查询出与该试卷相关的试题(一对多),查询出试题的答案及分数(一对多)。

SelfTestTitle 实体类,SelfTestQuestion实体类,SelfTestAnswer实体类。

技术分享
package org.system.entity.self;import java.util.List;import org.core.entity.BaseEntity;public class SelfTestTitle extends BaseEntity {    private Integer id;    private String name;    private String desc;    private String picUrl;    private List<SelfTestQuestion> questionList;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getDesc() {        return desc;    }    public void setDesc(String desc) {        this.desc = desc;    }    public String getPicUrl() {        return picUrl;    }    public void setPicUrl(String picUrl) {        this.picUrl = picUrl;    }            public List<SelfTestQuestion> getQuestionList() {        return questionList;    }    public void setQuestionList(List<SelfTestQuestion> questionList) {        this.questionList = questionList;    }    @Override    public Integer getPage() {        return super.getPage();    }    @Override    public Integer getRows() {        return super.getRows();    }}
View Code
技术分享
package org.system.entity.self;import java.util.List;import javax.validation.constraints.NotNull;import org.core.entity.BaseEntity;import org.hibernate.validator.constraints.NotBlank;import org.utils.spring.Groups;public class SelfTestQuestion extends BaseEntity {    private Integer id;        @NotNull(message = "{selfTestTitle.id.notnull.valid}", groups = { Groups.Insert.class })    private Integer titleId;    @NotBlank(message = "{question.notblank.valid}", groups = { Groups.Insert.class })    private String question;    @NotNull(message = "{viewOrder.notnull.valid}", groups = { Groups.Insert.class })    private Integer viewOrder;     private List<SelfTestAnswer> answersList;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public Integer getTitleId() {        return titleId;    }    public void setTitleId(Integer titleId) {        this.titleId = titleId;    }    public String getQuestion() {        return question;    }    public void setQuestion(String question) {        this.question = question;    }    public Integer getViewOrder() {        return viewOrder;    }    public void setViewOrder(Integer viewOrder) {        this.viewOrder = viewOrder;    }                    public List<SelfTestAnswer> getAnswersList() {        return answersList;    }    public void setAnswersList(List<SelfTestAnswer> answersList) {        this.answersList = answersList;    }    @Override    public Integer getPage() {        return super.getPage();    }    @Override    public Integer getRows() {        return super.getRows();    }}
View Code
技术分享
package org.system.entity.self;import org.core.entity.BaseEntity;public class SelfTestAnswer extends BaseEntity {    private Integer id;    private Integer questionId;    private String answer;    private Integer score;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public Integer getQuestionId() {        return questionId;    }    public void setQuestionId(Integer questionId) {        this.questionId = questionId;    }    public String getAnswer() {        return answer;    }    public void setAnswer(String answer) {        this.answer = answer;    }    public Integer getScore() {        return score;    }    public void setScore(Integer score) {        this.score = score;    }    @Override    public Integer getPage() {        return super.getPage();    }    @Override    public Integer getRows() {        return super.getRows();    }}
View Code
 1     <!-- 查询试卷详情 --> 2       <select id="queryOne"  parameterType="org.system.entity.self.SelfTestTitle"  resultMap="getSelfTestQuestionMap"> 3         select id, name, `desc` de, pic_url picUrl 4         from self_test_title 5         where id = #{id} 6       </select>  7        <!-- 定义结果集 试卷下的问题 --> 8     <resultMap type="map" id="getSelfTestQuestionMap"> 9           <result property="id" column="id" />10           <collection property="questionList" column="id" javaType="list" select="getSelfTestQuestions"/>11     </resultMap>12         13    <!-- 查询问题 -->14   <select id="getSelfTestQuestions"  parameterType="org.system.entity.self.SelfTestTitle"  resultMap="getSelfTestAnswersMap">15         select id, title_id titleId, question, view_order viewOrder16         from self_test_question17         where title_id = #{id}18   </select>  19   20     <!-- 定义结果集  问题下的答案 -->21       <resultMap type="map" id="getSelfTestAnswersMap">22           <result property="id" column="id"/>23           <collection property="answersList" column="id" javaType="list" select="getSelfTestAnswers"></collection>24       </resultMap>25    <!-- 查询问题 -->26     <select id="getSelfTestAnswers"  parameterType="org.system.entity.self.SelfTestAnswer"  resultType="map">27         select id ,question_id questionId,answer ,score from  self_test_answer where question_id =#{id}28     </select> 

property:属性名称
column:外键列
javaType:类型(可以是自己的实体类)
select:关联的查询语句
collection:一对多的标签
property:属性名称
column:外键列

查询出来的结果

技术分享

 

现在主要是要让他显示出来了!

 

Mybatis 级联查询 (一对多 )