码迷,mamicode.com
首页 > 其他好文 > 详细

一对多处理

时间:2020-01-10 22:04:31      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:结果   映射   可读性   一对多   ntb   属性   泛型   注意   poj   

一对多处理

按照查询嵌套处理

<resultMap id="TeacherStudent" type="Teacher">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!--复杂的属性需要单独处理   对象:association   集合:collection-->
    <collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId"/>
</resultMap>

<select id="getStudentByTeacherId" resultType="Student">
    select * from student where tid = #{tid}
</select>
<select id="getTeacher" resultMap="TeacherStudent">
    select * from teacher where id = #{tid}
</select>

按照结果嵌套处理

<resultMap id="TeacherStudent2" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <!--复杂的属性需要单独处理   对象:association   集合:collection-->
    <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 s.id sid, s.name sname, t.name tname, t.id tid
    from student s, teacher t
    where s.tid = t.id and t.id = #{tid}
</select>

小结

  1. 关联-association [多对一]
  2. 集合-collection [一对多]
  3. javaType : 用来指定实体类中属性的类型
  4. ofType : 用来指定映射到List或者集合中的pojo类型, 泛型中的约束类型

注意

  • 保证SQL的可读性, 尽量通俗易懂

  • 注意一对多和多对一中, 属性名和字段的问题

  • 善用日志排查问题

一对多处理

标签:结果   映射   可读性   一对多   ntb   属性   泛型   注意   poj   

原文地址:https://www.cnblogs.com/pinked/p/12178026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!