Open source for everyone, complex statement selection related to multiple tables: Advanced mapping of Mybatis

MyBatis was created with the idea that the database cannot always be what you want or need. We would like every database to be in good third normal form or BCNF, but unfortunately they are not always like that. It would be great if there was a database mapping model that perfectly suited all applications, but unfortunately there isn't one. ResultMap is MyBatis's answer to this problem.

For example, how do we map the following statement?

<!-- 非常复杂的语句 -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
  select
       B.id as blog_id,
       B.title as blog_title,
       B.author_id as blog_author_id,
       A.id as author_id,
       A.username as author_username,
       A.password as author_password,
       A.email as author_email,
       A.bio as author_bio,
       A.favourite_section as author_favourite_section,
       P.id as post_id,
       P.blog_id as post_blog_id,
       P.author_id as post_author_id,
       P.created_on as post_created_on,
       P.section as post_section,
       P.subject as post_subject,
       P.draft as draft,
       P.body as post_body,
       C.id as comment_id,
       C.post_id as comment_post_id,
       C.name as comment_name,
       C.comment as comment_text,
       T.id as tag_id,
       T.name as tag_name
  from Blog B
       left outer join Author A on B.author_id = A.id
       left outer join Post P on B.id = P.blog_id
       left outer join Comment C on P.id = C.post_id
       left outer join Post_Tag PT on PT.post_id = P.id
       left outer join Tag T on PT.tag_id = T.id
  where B.id = #{id}
</select>

You might want to map this to a smart object model, where this object represents a blog written by an author with many blog posts, each with zero or more comments and tags. Let's first look at the complete example below, which is a very complex result mapping (assuming author, blog, post, comment and tag are all type aliases). Don't stress, we'll explain it step by step. While it may seem daunting, it's actually very simple.

<!-- 非常复杂的结果映射 -->
<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>

resultMap Elements have many sub-elements and a structure worth exploring in depth. Below is resultMap a conceptual view of the elements.

result map (resultMap)
  • constructor - Used to inject results into the constructor when instantiating a class
    • idArg - ID parameter; marking results as IDs can help improve overall performance
    • arg - a normal result that will be injected into the constructor
  • id – An ID result; marking results as IDs can help improve overall performance
  • result – Ordinary results injected into fields or JavaBean properties
  • association – An association of complex types; many results will be wrapped into this type
    • Nested result maps – associations can be  resultMap elements, or references to other result maps
  • collection – A collection of complex types
    • Nested result maps – collections can be  resultMap elements, or references to other result maps
  • discriminator – Use the resulting value to decide which one to use resultMap
    • case – Result mapping based on certain values
      • Nested result map –  case is also a result map and therefore has the same structure and elements; or references other result maps
Property list of ResultMap
Attributes describe
id A unique identifier in the current namespace that identifies a result map.
type The fully qualified name of the class, or a type alias (see the table above for built-in type aliases).
autoMapping If this property is set, MyBatis will turn on or off automatic mapping for this result mapping. This property overrides the global property autoMappingBehavior. Default value: unset.

Best Practices It is best to build outcome mapping step by step. Unit testing can help a lot in this process. If you try to create a result map as huge as the example above at once, it is not only error-prone, but also difficult. So, start with the simplest form and iterate step by step. And don't forget about unit tests! Sometimes a framework behaves like a black box (open source or not). Therefore, to ensure that your implementation behaves as you expect, it's best to write unit tests. And unit testing can also play a big role when submitting bugs.

Each element is explained in detail in the next section.

id & result
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>

These elements are the basis for result mapping. Both the id  and  result  elements map a column value to an attribute or field of a simple data type (String, int, double, Date, etc.).

The only difference between the two is that  the attribute corresponding to the id element will be marked as the identifier of the object, which is used when comparing object instances. This can improve overall performance, especially when caching and nested result mappings (aka join mappings).

Both elements have some properties:

Id and Result properties
Attributes describe
property Field or attribute mapped to column results. If the JavaBean has a property with this name, that property will be used first. Otherwise MyBatis will look for a field with the given name. In either case, you can use the common dotted format for complex attribute navigation. For example, you could map something as simple as "username", or map to something complex as "address.street.number".
column The column name in the database, or an alias for the column. Normally, this is  resultSet.getString(columnName) the same as the parameters passed to the method.
javaType The fully qualified name of a Java class, or a type alias (see the table above for built-in type aliases). If you map to a JavaBean, MyBatis can usually infer the type. However, if you are mapping to a HashMap, you should specify the javaType explicitly to ensure that the behavior is consistent with expectations.
jdbcType JDBC type. For supported JDBC types, see "Supported JDBC Types" after this table. You only need to specify the JDBC type on columns that may be inserted, updated, and deleted and that allow null values. This is a requirement of JDBC, not MyBatis. If you are programming directly to JDBC, you need to specify this type for columns that can be nullable.
typeHandler We discussed the default type handler earlier. Using this attribute, you can override the default type handler. The value of this attribute is the fully qualified name of a type handler implementation class, or a type alias.
Supported JDBC types

For possible future usage scenarios, MyBatis supports the following JDBC types through the built-in jdbcType enumeration type.

BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED
TINYINT REAL VARCHAR BINARY BLOB NVARCHAR
SMALLINT DOUBLE LONGVARCHAR VARBINARY CLOB NCHAR
INTEGER NUMERIC DATE LONGVARBINARY BOOLEAN NCLOB
BIGINT DECIMAL TIME NULL CURSOR ARRAY
Construction method

By modifying object properties, the requirements of most Data Transfer Objects (DTO) and most domain models can be met. But there are situations where you want to use immutable classes. Generally speaking, tables containing references or data that rarely change or remain essentially unchanged are suitable for immutable classes. Constructor injection allows you to set property values ​​for a class during initialization without exposing public methods. MyBatis also supports private properties and private JavaBean properties to complete injection, but some people prefer injection through constructors. The constructor  element is born for this.

Take a look at the following constructor:

public class User {
   //...
   public User(Integer id, String username, int age) {
     //...
  }
//...
}

In order to inject results into a constructor, MyBatis needs to locate the corresponding constructor in some way. In the following example, MyBatis searches for a constructor that declares three formal parameters. The parameter types are given in the order of  java.lang.Integerjava.lang.String and  int .

<constructor>
   <idArg column="id" javaType="int"/>
   <arg column="username" javaType="String"/>
   <arg column="age" javaType="_int"/>
</constructor>

When you're dealing with a constructor with multiple parameters, it's easy to mess up the order of the arg elements. Starting in version 3.4.3, arg elements can be written in any order, provided the parameter names are specified. To refer to constructor parameters by name, you can add  @Param annotations or compile the  useActualParamName project with the '-parameters' compile option and enable the option (enabled by default). Here is an equivalent example, although the order of the second and third parameters in the function signature does not match the order of parameter declarations in the constructor element.

<constructor>
   <idArg column="id" javaType="int" name="id" />
   <arg column="age" javaType="_int" name="age" />
   <arg column="username" javaType="String" name="username" />
</constructor>

This can be omitted if a writable property of the same name and type exists  javaType .

The remaining attributes and rules are the same as for ordinary id and result elements.

Attributes describe
column The column name in the database, or an alias for the column. Normally, this is  resultSet.getString(columnName) the same as the parameters passed to the method.
javaType The fully qualified name of a Java class, or a type alias (see the table above for built-in type aliases). If you map to a JavaBean, MyBatis can usually infer the type. However, if you are mapping to a HashMap, you should specify the javaType explicitly to ensure that the behavior is consistent with expectations.
jdbcType JDBC type. For supported JDBC types, see "Supported JDBC Types" before this table. You only need to specify the JDBC type on columns that may be inserted, updated, and deleted and that allow null values. This is a requirement of JDBC, not MyBatis. If you are programming directly to JDBC, you need to specify this type for columns that may contain null values.
typeHandler We discussed the default type handler earlier. Using this attribute, you can override the default type handler. The value of this attribute is the fully qualified name of a type handler implementation class, or a type alias.
select The ID of the mapping statement used to load complex type properties, which retrieves data from the column specified in the column attribute, is passed as a parameter to this select statement. Please refer to related elements for details.
resultMap The ID of the result mapping, which maps nested result sets to an appropriate object tree. It can be used as an alternative to using additional select statements. It can map the results of multiple table join operations into a single  ResultSet. Such  ResultSet a result set will contain duplicate or partially duplicate data. In order to correctly map result sets into nested object trees, MyBatis allows you to "concatenate" result maps in order to solve the problem of nested result sets. For more information, please refer to the associated elements below.
name The name of the constructor method parameter. Starting with version 3.4.3, you can write arg elements in any order by specifying specific parameter names. See explanation above.
association
<association property="author" column="blog_author_id" javaType="Author">
  <id property="id" column="author_id"/>
  <result property="username" column="author_username"/>
</association>

The association element handles "has-a" type relationships. For example, in our example, a blog has one user. Association result mapping works much like other types of mapping. You need to specify the target attribute name and attribute javaType(in many cases MyBatis can infer it by itself). If necessary, you can also set the JDBC type. If you want to override the process of obtaining the result value, you can also set the type processor.

The difference with associations is that you need to tell MyBatis how to load the association. MyBatis has two different ways to load associations:

  • Nested Select query: Load the desired complex type by executing another SQL mapping statement.
  • Nested result mapping: Use nested result mapping to handle duplicate subsets of join results.

First, let's take a look at the properties of this element. You will find that compared with the ordinary result map, it only differs in the select and resultMap attributes.

Attributes describe
property Field or attribute mapped to column results. If the JavaBean used for matching has a property with the given name, it will be used. Otherwise MyBatis will look for a field with the given name. In either case, you can use the usual dotted format for complex attribute navigation. For example, you could map something as simple as "username", or map to something complex as "address.street.number".
javaType The fully qualified name of a Java class, or a type alias (see the table above for built-in type aliases). If you map to a JavaBean, MyBatis can usually infer the type. However, if you are mapping to a HashMap, you should specify the javaType explicitly to ensure that the behavior is consistent with expectations.
jdbcType JDBC type. For supported JDBC types, see "Supported JDBC Types" before this table. You only need to specify the JDBC type on columns that may be inserted, updated, and deleted and that allow null values. This is a requirement of JDBC, not MyBatis. If you are programming directly to JDBC, you need to specify this type for columns that may contain null values.
typeHandler We discussed the default type handler earlier. Using this attribute, you can override the default type handler. The value of this attribute is the fully qualified name of a type handler implementation class, or a type alias.
Associated nested Select queries
Attributes describe
column 数据库中的列名,或者是列的别名。一般情况下,这和传递给 resultSet.getString(columnName) 方法的参数一样。 注意:在使用复合主键的时候,你可以使用 column="{prop1=col1,prop2=col2}" 这样的语法来指定多个传递给嵌套 Select 查询语句的列名。这会使得 prop1 和 prop2 作为参数对象,被设置为对应嵌套 Select 语句的参数。
select 用于加载复杂类型属性的映射语句的 ID,它会从 column 属性指定的列中检索数据,作为参数传递给目标 select 语句。 具体请参考下面的例子。注意:在使用复合主键的时候,你可以使用 column="{prop1=col1,prop2=col2}" 这样的语法来指定多个传递给嵌套 Select 查询语句的列名。这会使得 prop1 和 prop2 作为参数对象,被设置为对应嵌套 Select 语句的参数。
fetchType 可选的。有效值为 lazy 和 eager。 指定属性后,将在映射中忽略全局配置参数 lazyLoadingEnabled,使用属性的值。

示例:

<resultMap id="blogResult" type="Blog">
  <association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>

<select id="selectBlog" resultMap="blogResult">
  SELECT * FROM BLOG WHERE ID = #{id}
</select>

<select id="selectAuthor" resultType="Author">
  SELECT * FROM AUTHOR WHERE ID = #{id}
</select>

就是这么简单。我们有两个 select 查询语句:一个用来加载博客(Blog),另外一个用来加载作者(Author),而且博客的结果映射描述了应该使用 selectAuthor 语句加载它的 author 属性。

其它所有的属性将会被自动加载,只要它们的列名和属性名相匹配。

这种方式虽然很简单,但在大型数据集或大型数据表上表现不佳。这个问题被称为“N+1 查询问题”。 概括地讲,N+1 查询问题是这样子的:

  • 你执行了一个单独的 SQL 语句来获取结果的一个列表(就是“+1”)。
  • 对列表返回的每条记录,你执行一个 select 查询语句来为每条记录加载详细信息(就是“N”)。

这个问题会导致成百上千的 SQL 语句被执行。有时候,我们不希望产生这样的后果。

好消息是,MyBatis 能够对这样的查询进行延迟加载,因此可以将大量语句同时运行的开销分散开来。 然而,如果你加载记录列表之后立刻就遍历列表以获取嵌套的数据,就会触发所有的延迟加载查询,性能可能会变得很糟糕。

所以还有另外一种方法。

关联的嵌套结果映射
属性 描述
resultMap 结果映射的 ID,可以将此关联的嵌套结果集映射到一个合适的对象树中。 它可以作为使用额外 select 语句的替代方案。它可以将多表连接操作的结果映射成一个单一的 ResultSet。这样的 ResultSet 有部分数据是重复的。 为了将结果集正确地映射到嵌套的对象树中, MyBatis 允许你“串联”结果映射,以便解决嵌套结果集的问题。使用嵌套结果映射的一个例子在表格以后。
columnPrefix 当连接多个表时,你可能会不得不使用列别名来避免在 ResultSet 中产生重复的列名。指定 columnPrefix 列名前缀允许你将带有这些前缀的列映射到一个外部的结果映射中。 详细说明请参考后面的例子。
notNullColumn 默认情况下,在至少一个被映射到属性的列不为空时,子对象才会被创建。 你可以在这个属性上指定非空的列来改变默认行为,指定后,Mybatis 将只在这些列中任意一列非空时才创建一个子对象。可以使用逗号分隔来指定多个列。默认值:未设置(unset)。
autoMapping 如果设置这个属性,MyBatis 将会为本结果映射开启或者关闭自动映射。 这个属性会覆盖全局的属性 autoMappingBehavior。注意,本属性对外部的结果映射无效,所以不能搭配 select 或 resultMap 元素使用。默认值:未设置(unset)。

之前,你已经看到了一个非常复杂的嵌套关联的例子。 下面的例子则是一个非常简单的例子,用于演示嵌套结果映射如何工作。 现在我们将博客表和作者表连接在一起,而不是执行一个独立的查询语句,就像这样:

<select id="selectBlog" resultMap="blogResult">
  select
    B.id            as blog_id,
    B.title         as blog_title,
    B.author_id     as blog_author_id,
    A.id            as author_id,
    A.username      as author_username,
    A.password      as author_password,
    A.email         as author_email,
    A.bio           as author_bio
  from Blog B left outer join Author A on B.author_id = A.id
  where B.id = #{id}
</select>

注意查询中的连接,以及为确保结果能够拥有唯一且清晰的名字,我们设置的别名。 这使得进行映射非常简单。现在我们可以映射这个结果:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/>
</resultMap>

<resultMap id="authorResult" type="Author">
  <id property="id" column="author_id"/>
  <result property="username" column="author_username"/>
  <result property="password" column="author_password"/>
  <result property="email" column="author_email"/>
  <result property="bio" column="author_bio"/>
</resultMap>

在上面的例子中,你可以看到,博客(Blog)作者(author)的关联元素委托名为 “authorResult” 的结果映射来加载作者对象的实例。

非常重要: id 元素在嵌套结果映射中扮演着非常重要的角色。你应该总是指定一个或多个可以唯一标识结果的属性。 虽然,即使不指定这个属性,MyBatis 仍然可以工作,但是会产生严重的性能问题。 只需要指定可以唯一标识结果的最少属性。显然,你可以选择主键(复合主键也可以)。

现在,上面的示例使用了外部的结果映射元素来映射关联。这使得 Author 的结果映射可以被重用。 然而,如果你不打算重用它,或者你更喜欢将你所有的结果映射放在一个具有描述性的结果映射元素中。 你可以直接将结果映射作为子元素嵌套在内。这里给出使用这种方式的等效例子:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
  </association>
</resultMap>

那如果博客(blog)有一个共同作者(co-author)该怎么办?select 语句看起来会是这样的:

<select id="selectBlog" resultMap="blogResult">
  select
    B.id            as blog_id,
    B.title         as blog_title,
    A.id            as author_id,
    A.username      as author_username,
    A.password      as author_password,
    A.email         as author_email,
    A.bio           as author_bio,
    CA.id           as co_author_id,
    CA.username     as co_author_username,
    CA.password     as co_author_password,
    CA.email        as co_author_email,
    CA.bio          as co_author_bio
  from Blog B
  left outer join Author A on B.author_id = A.id
  left outer join Author CA on B.co_author_id = CA.id
  where B.id = #{id}
</select>

回忆一下,Author 的结果映射定义如下:

<resultMap id="authorResult" type="Author">
  <id property="id" column="author_id"/>
  <result property="username" column="author_username"/>
  <result property="password" column="author_password"/>
  <result property="email" column="author_email"/>
  <result property="bio" column="author_bio"/>
</resultMap>

由于结果中的列名与结果映射中的列名不同。你需要指定 columnPrefix 以便重复使用该结果映射来映射 co-author 的结果。

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <association property="author"
    resultMap="authorResult" />
  <association property="coAuthor"
    resultMap="authorResult"
    columnPrefix="co_" />
</resultMap>
关联的多结果集(ResultSet)
属性 描述
column 当使用多个结果集时,该属性指定结果集中用于与 foreignColumn 匹配的列(多个列名以逗号隔开),以识别关系中的父类型与子类型。
foreignColumn 指定外键对应的列名,指定的列将与父类型中 column 的给出的列进行匹配。
resultSet 指定用于加载复杂类型的结果集名字。

从版本 3.2.3 开始,MyBatis 提供了另一种解决 N+1 查询问题的方法。

某些数据库允许存储过程返回多个结果集,或一次性执行多个语句,每个语句返回一个结果集。 我们可以利用这个特性,在不使用连接的情况下,只访问数据库一次就能获得相关数据。

在例子中,存储过程执行下面的查询并返回两个结果集。第一个结果集会返回博客(Blog)的结果,第二个则返回作者(Author)的结果。

SELECT * FROM BLOG WHERE ID = #{id}

SELECT * FROM AUTHOR WHERE ID = #{id}

在映射语句中,必须通过 resultSets 属性为每个结果集指定一个名字,多个名字使用逗号隔开。

<select id="selectBlog" resultSets="blogs,authors" resultMap="blogResult" statementType="CALLABLE">
  {call getBlogsAndAuthors(#{id,jdbcType=INTEGER,mode=IN})}
</select>

现在我们可以指定使用 “authors” 结果集的数据来填充 “author” 关联:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="id" />
  <result property="title" column="title"/>
  <association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id">
    <id property="id" column="id"/>
    <result property="username" column="username"/>
    <result property="password" column="password"/>
    <result property="email" column="email"/>
    <result property="bio" column="bio"/>
  </association>
</resultMap>

你已经在上面看到了如何处理“有一个”类型的关联。但是该怎么处理“有很多个”类型的关联呢?这就是我们接下来要介绍的。

集合
<collection property="posts" ofType="domain.blog.Post">
  <id property="id" column="post_id"/>
  <result property="subject" column="post_subject"/>
  <result property="body" column="post_body"/>
</collection>

集合元素和关联元素几乎是一样的,它们相似的程度之高,以致于没有必要再介绍集合元素的相似部分。 所以让我们来关注它们的不同之处吧。

我们来继续上面的示例,一个博客(Blog)只有一个作者(Author)。但一个博客有很多文章(Post)。 在博客类中,这可以用下面的写法来表示:

private List<Post> posts;

要像上面这样,映射嵌套结果集合到一个 List 中,可以使用集合元素。 和关联元素一样,我们可以使用嵌套 Select 查询,或基于连接的嵌套结果映射集合。

集合的嵌套 Select 查询

首先,让我们看看如何使用嵌套 Select 查询来为博客加载文章。

<resultMap id="blogResult" type="Blog">
  <collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
</resultMap>

<select id="selectBlog" resultMap="blogResult">
  SELECT * FROM BLOG WHERE ID = #{id}
</select>

<select id="selectPostsForBlog" resultType="Post">
  SELECT * FROM POST WHERE BLOG_ID = #{id}
</select>

你可能会立刻注意到几个不同,但大部分都和我们上面学习过的关联元素非常相似。 首先,你会注意到我们使用的是集合元素。 接下来你会注意到有一个新的 “ofType” 属性。这个属性非常重要,它用来将 JavaBean(或字段)属性的类型和集合存储的类型区分开来。 所以你可以按照下面这样来阅读映射:

<collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>

读作: “posts 是一个存储 Post 的 ArrayList 集合”

在一般情况下,MyBatis 可以推断 javaType 属性,因此并不需要填写。所以很多时候你可以简略成:

<collection property="posts" column="id" ofType="Post" select="selectPostsForBlog"/>
集合的嵌套结果映射

现在你可能已经猜到了集合的嵌套结果映射是怎样工作的——除了新增的 “ofType” 属性,它和关联的完全相同。

首先, 让我们看看对应的 SQL 语句:

<select id="selectBlog" resultMap="blogResult">
  select
  B.id as blog_id,
  B.title as blog_title,
  B.author_id as blog_author_id,
  P.id as post_id,
  P.subject as post_subject,
  P.body as post_body,
  from Blog B
  left outer join Post P on B.id = P.blog_id
  where B.id = #{id}
</select>

我们再次连接了博客表和文章表,并且为每一列都赋予了一个有意义的别名,以便映射保持简单。 要映射博客里面的文章集合,就这么简单:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
  </collection>
</resultMap>

再提醒一次,要记得上面 id 元素的重要性,如果你不记得了,请阅读关联部分的相关部分。

如果你喜欢更详略的、可重用的结果映射,你可以使用下面的等价形式:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post" resultMap="blogPostResult" columnPrefix="post_"/>
</resultMap>

<resultMap id="blogPostResult" type="Post">
  <id property="id" column="id"/>
  <result property="subject" column="subject"/>
  <result property="body" column="body"/>
</resultMap>
集合的多结果集(ResultSet)

像关联元素那样,我们可以通过执行存储过程实现,它会执行两个查询并返回两个结果集,一个是博客的结果集,另一个是文章的结果集:

SELECT * FROM BLOG WHERE ID = #{id}

SELECT * FROM POST WHERE BLOG_ID = #{id}

在映射语句中,必须通过 resultSets 属性为每个结果集指定一个名字,多个名字使用逗号隔开。

<select id="selectBlog" resultSets="blogs,posts" resultMap="blogResult">
  {call getBlogsAndPosts(#{id,jdbcType=INTEGER,mode=IN})}
</select>

我们指定 “posts” 集合将会使用存储在 “posts” 结果集中的数据进行填充:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="id" />
  <result property="title" column="title"/>
  <collection property="posts" ofType="Post" resultSet="posts" column="id" foreignColumn="blog_id">
    <id property="id" column="id"/>
    <result property="subject" column="subject"/>
    <result property="body" column="body"/>
  </collection>
</resultMap>

注意 对关联或集合的映射,并没有深度、广度或组合上的要求。但在映射时要留意性能问题。 在探索最佳实践的过程中,应用的单元测试和性能测试会是你的好帮手。 而 MyBatis 的好处在于,可以在不对你的代码引入重大变更(如果有)的情况下,允许你之后改变你的想法。

高级关联和集合映射是一个深度话题。文档的介绍只能到此为止。配合少许的实践,你会很快了解全部的用法。

Guess you like

Origin blog.csdn.net/weixin_44821114/article/details/131975450