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();
} }
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();
}
}
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();
}
}
     <!-- 查询试卷详情 -->
<select id="queryOne" parameterType="org.system.entity.self.SelfTestTitle" resultMap="getSelfTestQuestionMap">
select id, name, `desc` de, pic_url picUrl
from self_test_title
where id = #{id}
</select>
<!-- 定义结果集 试卷下的问题 -->
<resultMap type="map" id="getSelfTestQuestionMap">
<result property="id" column="id" />
<collection property="questionList" column="id" javaType="list" select="getSelfTestQuestions"/>
</resultMap> <!-- 查询问题 -->
<select id="getSelfTestQuestions" parameterType="org.system.entity.self.SelfTestTitle" resultMap="getSelfTestAnswersMap">
select id, title_id titleId, question, view_order viewOrder
from self_test_question
where title_id = #{id}
</select> <!-- 定义结果集 问题下的答案 -->
<resultMap type="map" id="getSelfTestAnswersMap">
<result property="id" column="id"/>
<collection property="answersList" column="id" javaType="list" select="getSelfTestAnswers"></collection>
</resultMap>
<!-- 查询问题 -->
<select id="getSelfTestAnswers" parameterType="org.system.entity.self.SelfTestAnswer" resultType="map">
select id ,question_id questionId,answer ,score from self_test_answer where question_id =#{id}
</select>

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

查询出来的结果

Mybatis 级联查询 (一对多 )

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

上一篇:windows cmd命令行下创建文件和文件夹


下一篇:mybatis级联查询,多对一查询问题