Mybatis 一对多 简单映射配置

只需在一对多的 “一” Model中定义一个list集合:

public class SelectQuestion{

    // 主键ID
    private Integer id;

    private String name;
    //选项列表
    private List<SelectOption> optionList;
//省略了getter和setter方法

然后在一对多的 “一” Mapper定义

<resultMap id="BaseResultMap" type="com.model.SelectQuestion">
        <id column="id" property="id"/>
        <result column="task_id" property="taskId"/>
        <result column="select_content" property="selectContent"/>
        <collection property="optionList" column="id" ofType="com.model.SelectOption">
            <id property="id" column="option_id"/>
            <result property="taskId" column="task_id"/>
            <result property="selectId" column="select_id"/>
            <result property="optionFlag" column="option_flag"/>
            <result property="optionContent" column="option_content"/>
        </collection>
    </resultMap>

<select id="findListSelectQuestionNoAnswer" resultMap="BaseResultMap">
        SELECT a.*,b.id AS option_id,b.select_id,b.option_flag,b.option_content FROM test_select_question AS a
        LEFT JOIN test_select_option b ON a.id=b.select_id
        <include refid="findByQoCondition"></include>
    </select>

注意下,如果2个表的查询,字段相同,请设置别名。

至于mapper.java 就和普通的单表查询一样写了

原文地址:https://www.cnblogs.com/dwb91/p/9644798.html