Mybatis一对一,一对多

一对一

假如有student表(学生表)和student_card表(学生证表)。

student表中有一个字段self_card用来查student_card,student_card表中有一个student_id用来查student。

在Student的pojo类中,成员self_card被替换成StudentCard的pojo类,通过查询student_id将Student中的StudentCard补全。

student表如果没有self_card这个字段对于pojo类来说是无关紧要的,因为pojo类可以重新增加其对应的成员再对其赋值。

这种情况在Mybatis中可以在StudentMapper的<resultMap>中添加

<association property="self_card" column="id" 
    	select="mytatis.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId" />

就可以获取Student的所有信息。

一对多

如果一个学生对应多门课程,这时候称为一对多,而每门课程对其具体的课程信息就是一对一。
一对多的级联就需要用<collection>

<collection property="studentLecturesList" column="id" 
  	    select="mytatis.mapper.StudentLectureMapper.findStudentLectureByStuId" />

一对一还是用上面的<association>
具体代码请点击源码

posted @ 2018-11-20 15:05  bihang  阅读(172)  评论(0编辑  收藏  举报