mybatis 一对多

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cj.dao.TeacherMapper">
    
<!--    连接查询-->
    <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 tid = #{tid}
    </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="getTeacher2" resultMap="teacherStudent2">
        select * from mybatis.teacher where id = #{tid};
    </select>
    <resultMap id="teacherStudent2" type="teacher">
        <result property="id" column="id"/>
        <collection property="students" javaType="ArrayList" ofType="student" select="getStudent" column="id"/>
    </resultMap>
    <select id="getStudent" resultType="student">
        select * from mybatis.student where tid = #{tid};
    </select>


</mapper>

 

posted @ 2021-12-05 16:30  写代码的小哥哥  阅读(35)  评论(0)    收藏  举报