1.多对多
老师表------中间表-------学生表
中间表存储老师和学生的关系
2.注意事项
写sql的时候先在Navicat中测试后再写入mybatis会更好一些。
连表查询 示例:
<resultMap id="studentMap" type="Student">
<id property="id" column="id" />
<result property="name" column="name"/>
<result property="age" column="age"/>
<collection property="teachers" column="id" select="mapper.TeacherMapper.selectTeacher"/>
</resultMap>
<select id="selectStudent" resultMap="studentMap">
SELECT * FROM student WHERE id = #{id}
</select>
<select id="selectTeacher" resultType="domain.Teacher">
SELECT id,name FROM teacher t JOIN relation r ON t.id=r.teacher_id WHERE student_id =#{id}
</select>
Comments | NOTHING