第十一章、一对多
比如一个老师拥有多个学生
按照结果嵌套处理
1 <resultMap id="TeacherStudent" type="Teacher"> 2 <result property="id" column="tid"/> 3 <result property="name" column="tname"/> 4 <collection property="students" ofType="Student"> 5 <result property="id" column="sid"/> 6 <result property="name" column="sname"/> 7 <result property="tid" column="tid"/> 8 </collection> 9 </resultMap> 10 11 <select id="getTeacher" resultMap="TeacherStudent"> 12 select s.id sid, s.name sname,t.name tname ,t.id tid 13 from teacher t,student s 14 where t.id = s.tid and t.id = #{tid} 15 </select>
按照查询嵌套处理
1 <resultMap id="TeacherStudent2" type="Teacher"> 2 <collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudent"> 3 4 </collection> 5 </resultMap> 6 7 8 9 <select id="getTeacher2" resultMap="TeacherStudent2"> 10 select * from teacher where id = #{tid} 11 </select> 12 13 <select id="getStudent" resultType="Student"> 14 select * from student where tid = #{tid} 15 </select>
小结
-
关联 -association [多对一]
-
集合 -collectio n【一对多】
-
javaType 用来指定实体类中属性的类型
-
ofType 用来指定映射到list 或者集合中的pojo类型, 泛型中的约束类型
注意点
-
保证SQL的可读性,尽量保证通俗易懂
-
注意一对多 和多对一中,属性名和字段名的wenti如果问题不好排查错误,可以使用日志,建议使用Log4j
面试高频:
-
-
InnoDB底层原理
-
索引
-

浙公网安备 33010602011771号