一对多集合映射
collection 元素处理一对多关联关系,将多条关联记录映射到主对象的集合属性中。
collection 基本语法
XML
<resultMap id="authorResultMap" type="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
<collection property="books" ofType="Book">
<id property="id" column="book_id"/>
<result property="title" column="book_title"/>
<result property="price" column="book_price"/>
</collection>
</resultMap>
| 属性 | 说明 |
|---|---|
property | 集合属性名 |
ofType | 集合中元素的类型(关键属性) |
javaType | 集合类型(可选,默认 ArrayList) |
column | 传递给子查询的列(子查询模式) |
select | 子查询 ID |
Join 方式映射
Java
public class Author {
private Integer id;
private String name;
private List<Book> books;
}
public class Book {
private Integer id;
private String title;
private Double price;
}
XML
<resultMap id="authorBooksResult" type="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
<collection property="books" ofType="Book">
<id property="id" column="book_id"/>
<result property="title" column="book_title"/>
<result property="price" column="book_price"/>
</collection>
</resultMap>
<select id="selectAuthorWithBooks" resultMap="authorBooksResult">
SELECT a.id author_id, a.name author_name,
b.id book_id, b.title book_title, b.price book_price
FROM author a
LEFT JOIN book b ON a.id = b.author_id
WHERE a.id = #{id}
</select>
LEFT JOIN 保证即使作者没有书籍,作者信息也能正常返回。
子查询方式
XML
<resultMap id="authorResultMap" type="Author">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="books"
column="id"
ofType="Book"
select="selectBooksByAuthor"/>
</resultMap>
<select id="selectAuthor" resultMap="authorResultMap">
SELECT * FROM author WHERE id = #{id}
</select>
<select id="selectBooksByAuthor" resultType="Book">
SELECT * FROM book WHERE author_id = #{authorId}
</select>
ofType vs javaType
| 属性 | 说明 | 示例 |
|---|---|---|
ofType | 集合元素的泛型类型 | List<Book> 中为 Book |
javaType | 集合本身的类型 | ArrayList、LinkedList |
XML
<!-- ofType 指定集合元素类型(常用) -->
<collection property="books" ofType="Book">
<!-- javaType 指定集合实现类(可选) -->
<collection property="books" ofType="Book" javaType="java.util.LinkedList">
绝大多数场景只需配置
ofType,javaType使用默认 ArrayList 即可。
引用外部 resultMap
XML
<resultMap id="bookResult" type="Book">
<id property="id" column="book_id"/>
<result property="title" column="book_title"/>
<result property="price" column="book_price"/>
<result property="publishDate" column="publish_date"/>
</resultMap>
<resultMap id="authorResultMap" type="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
<collection property="books"
resultMap="bookResult"
columnPrefix="book_"/>
</resultMap>
嵌套多层集合
XML
<resultMap id="categoryResult" type="Category">
<id property="id" column="cat_id"/>
<result property="name" column="cat_name"/>
<collection property="articles" ofType="Article">
<id property="id" column="art_id"/>
<result property="title" column="art_title"/>
<collection property="comments" ofType="Comment">
<id property="id" column="cmt_id"/>
<result property="content" column="cmt_content"/>
</collection>
</collection>
</resultMap>
多层嵌套性能随深度增加而下降,建议最多不超过三层嵌套。
要点总结
- collection 用于一对多关联映射,将多条记录映射为集合。
ofType指定集合元素的 Java 类型,是必填属性。- Join 方式一次查询完成,性能优于子查询。
- 子查询方式支持延迟加载,适合按需获取集合数据。
- 可嵌套多层集合,但建议不超过三层。
- 引用外部 resultMap 配合
columnPrefix简化配置。
📝 发现内容有误?点击此处直接编辑