Mybatis- 一对多的处理

按结果嵌套处理

<!--第一种查询方法:按结果嵌套处理-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid, s.name sname, t.name tname, t.id tid
        from mybatis.student s, mybatis.teacher t
        where s.tid = t.id and t.id=#{id};
    </select>

    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

按查询嵌套处理

 <!--第二种查询方法:按查询嵌套处理-->
    <select id="getTeacher1" resultMap="TeacherStudent2">
        select * from mybatis.teacher where id = #{id};
    </select>

    <select id="getStudent" resultType="Student">
        select * from mybatis.student where tid = #{tid};
    </select>

    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudent" column="id"/>
    </resultMap>
posted @ 2021-07-08 16:32  withLevi  阅读(61)  评论(0编辑  收藏  举报