使用Mybatis进行递归查询,查询得到的数据:嵌套集合
1.实体类Vo:
public class AssessmentVo {
/**
* 题目序号
*/
private String questionId;
/**
* 题目内容
*/
private String questionName;
/**
* 选项列表
*/
private List<AnswerVo> AnswerList;
}
实体类记得添加get/set方法
2.mapper.java
public interface AssessmentVoMapper {
/**
* 查询题目及选项
* @return
*/
public List<AssessmentVo> list();
}
3.mapper.xml
<!-- 递归查询选项-->
<resultMap type="AssessmentVo" id="AssessmentVoResult">
<result property="questionId" column="question_id"/>
<result property="questionName" column="question_name"/>
<result property="questionType" column="question_type"/>
<collection property="AnswerList" ofType="AnswerVo" column="question_id" select="getAnswer"/>
</resultMap>
<select id="list" resultMap="AssessmentVoResult">
SELECT
*
FROM
assessment
</select>
<select id="getAnswer" resultType="AnswerVo">
SELECT
*
FROM
answer
WHERE
question_id= #{question_id}
</select>