全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-20 6 分钟 ✍️ juanwangdev

一对多集合映射

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集合本身的类型ArrayListLinkedList
XML
<!-- ofType 指定集合元素类型(常用) -->
<collection property="books" ofType="Book">

<!-- javaType 指定集合实现类(可选) -->
<collection property="books" ofType="Book" javaType="java.util.LinkedList">

绝大多数场景只需配置 ofTypejavaType 使用默认 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 简化配置。

📝 发现内容有误?点击此处直接编辑

← 上一篇 一对一关联映射
下一篇 → 多对多关联查询
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库