功能

表示该属性为“多”的一方

如教室与学生关系中的学生

使用

定义时,该属性应为集合

1
List<Student> students;

已知t_class(id,name)t_student(id,name,cid),外键为t_student.cid=t_class.id

设置实体clazz: Class(Integer id, String name, List<Student> students)student: Student(Integer id, String name)

用法与association基本一致

其他属性及用法参考association

collectionjavaType应该为ArrayList等列表类型(可省略),ofType是列表内的类型

属性 功能 举例 说明
ofType 列表内的类型 Student
1
2
3
4
5
6
7
8
9
10
11
12
<select parameterType="int" id="selectByClassId" resultMap="resultStudent">
select id, name
from t_student
where cid={#cid}
</select>
<select parameterType="int" id="selectClass" resultMap="resultWithClazz">
select id, name from user where id=#{id}
</select>

<resultMap id="resultWithClazz" type="Student" extends="resultStudent">
<collection property="students" column="id" ofType="Student" select="selectByClassId"/>
</resultMap>