多对多
学生-老师
<?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.sw.dao.StudentDao">
<select id="findStudentById" parameterType="int" resultMap="studentAndTeacherInfo">
select * from student where sid=#{sid};
</select>
<select id="findAllStudent" resultMap="studentAndTeacherInfo">
select * from student;
</select>
<!--自定义返回结果集-->
<resultMap id="studentAndTeacherInfo" type="Student">
<id column="sid" property="sid"></id>
<result column="sname" property="sname"></result>
<collection property="teachers" ofType="Teacher" select="findTeachersBySid" column="sid">
<id column="tid" property="tid"></id>
<result column="tname" property="tname"></result>
</collection>
</resultMap>
<!--通过学生的编号和第三方关系表关联查询出对应的老师信息-->
<select id="findTeachersBySid" parameterType="int" resultType="Teacher">
select * from teacher t inner join stu_tea st on t.tid=st.tid where st.sid=#{sid}
</select>
</mapper>