Mybatis一对多查询,复合查询,resultMap标签中的collection标签
一.表结构:
班级表 (classes)
字段 | 类型 | 注释 |
---|---|---|
id | bigint | id |
classes_name | varchar | 班级名字 |
学生表 (student)
字段 | 类型 | 注释 |
---|---|---|
id | bigint | id |
student_name | varchar | 名字 |
classes_id | bigint | 班级id |
一.实体类:
班级实体类 (Classes)
public class Classes{
private Long id;
private String classesName;
private List<Student> studentList;//子表列表
}
学生实体类 (Student)
public class Student{
private Long id;
private String studentName;
private Long classesId;
}
三.xml写法:
<resultMap id="classesListVo" type="com.xx.xx.xx.Classes">
<id property="id" column="id" jdbcType="INTEGER"></id>
<result property="classesName" column="classes_name" jdbcType="VARCHAR"></result>
<collection property="studentList" javaType="ArrayList" ofType="com.xx.xx.xx.Student">
<id property="id" column="template_id" jdbcType="INTEGER"/>
<result property="studentName" column="student_name" jdbcType="VARCHAR"/>
</collection>
</resultMap>
<select id="findList" resultMap="classesListVo" >
select
a.id,
a.classes_name,
b.id as studentId,
b.student_name as studentName
from classes a
left join student b on b.classes_id = a.id
</select>
四.结果:
版权声明:本文为weixin_40991408原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。