0
点赞
收藏
分享

微信扫一扫

后端框架的学习----mybatis框架(9、多对一处理和一对多处理)


9、多对一处理和一对多处理

#多对一

 <!--按照结果集嵌套查询-->
<select id="getAllStudent1" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s ,teacher t
where s.tid=t.id;
</select>

<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"></result>
</association>
</resultMap>

#一对多


<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname
from student as s ,teacher as t
where s.tid=t.id and t.id=#{tid};
</select>

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

##小结
1、关联-asscciation【多对一】
2、集合-collection 【一对多】
3、JavaType & offType

  • javaType:用来指定实体类中属性的类型
  • ofType:用来指定映射到List或者集合中的pojo类型,泛型中的约束类型


举报

相关推荐

0 条评论