Mybatis 3.1中 Mapper XML 文件 的学习详解

MyBatis 真正的力量是在映射语句中。这里是奇迹发生的地方。对于所有的力量,SQL 映射的 XML 文件是相当的简单。当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 95%的代码量。MyBatis 的构建就是聚焦于 SQL 的,使其远离于普通的方式。

SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序):

  • cache – 配置给定命名空间的缓存。
  • cache-ref – 从其他命名空间引用缓存配置。
  • resultMap – 最复杂,也是最有力量的元素,用来描述如何从数据库结果集中来加载你的对象。
  • parameterMap – 已经被废弃了!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除。这里不会记录。
  • sql – 可以重用的 SQL 块,也可以被其他语句引用。
  • insert – 映射插入语句
  • update – 映射更新语句
  • delete – 映射删除语句
  • select – 映射查询语句

下一部分将从语句本身开始来描述每个元素的细节。

select

查询语句是使用 MyBatis 时最常用的元素之一。直到你从数据库取出数据时才会发现将数据存在数据库中是多么的有价值, 所以许多应用程序查询要比更改数据多的多。对于每次插入,更新或删除,那也会有很多的查询。这是 MyBatis 的一个基本原则,也是将重心和努力放到查询和结果映射的原因。对简单类别的查询元素是非常简单的。比如:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectPerson"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"hashmap"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   SELECT * FROM PERSON WHERE ID = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

这个语句被称作 selectPerson, 使用一个 int (或 Integer) 类型的参数, 并返回一个 HashMap 类型的对象,其中的键是列名,值是列对应的值。

注意参数注释:

<span style="padding: 0px; margin: 0px;">#{id}</span>

这就告诉 MyBatis 创建一个预处理语句参数。使用 JDBC, 这样的一个参数在 SQL 中会由一个“?”来标识,并被传递到一个新的预处理语句中,就像这样:

<span style="padding: 0px; margin: 0px;">// Similar JDBC code, NOT MyBatis…</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> selectPerson </span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">"SELECT * FROM PERSON WHERE ID=?"</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">PreparedStatement</span><span style="padding: 0px; margin: 0px;"> ps </span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;"> conn</span><span style="padding: 0px; margin: 0px;">.</span><span style="padding: 0px; margin: 0px;">prepareStatement</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">selectPerson</span><span style="padding: 0px; margin: 0px;">);</span><span style="padding: 0px; margin: 0px;"> ps</span><span style="padding: 0px; margin: 0px;">.</span><span style="padding: 0px; margin: 0px;">setInt</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">1</span><span style="padding: 0px; margin: 0px;">,</span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">);</span>

当然, 这需要很多单独的 JDBC 的代码来提取结果并将它们映射到对象实例中, 这就是 MyBatis 节省你时间的地方。我们需要深入了解参数和结果映射。那些细节部分我们下面来了解。

select 元素有很多属性允许你配置,来决定每条语句的作用细节。

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectPerson"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">parameterMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"deprecated"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"hashmap"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"personResultMap"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"false"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">useCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">timeout</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"10000"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">fetchSize</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"256"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">statementType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"PREPARED"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">resultSetType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"FORWARD_ONLY"</span><span style="padding: 0px; margin: 0px;">></span>
Select Attributes
属性描述
id在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType将会传入这条语句的参数类的完全限定名或别名。
parameterMap这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。
resultType从这条语句中返回的期望类型的类的完全限定名或别名。注意集合情形,那应该是集合可以包含的类型,而不能是集合本身。使用 resultType 或 resultMap,但不能同时使用。
resultMap命名引用外部的 resultMap。返回 map 是 MyBatis 最具力量的特性, 对其有一个很好的理解的话, 许多复杂映射的情形就能被解决了。使用 resultMap 或 resultType,但不能同时使用。
flushCache将其设置为 true,不论语句什么时候被带哦用,都会导致缓存被清空。默认值:false。
useCache将其设置为 true, 将会导致本条语句的结果被缓存。默认值: true。
timeout这个设置驱动程序等待数据库返回请求结果,并抛出异常时间的最大等待值。默认不设置(驱动自行处理)
fetchSize这是暗示驱动程序每次批量返回的结果行数。默认不设置(驱动自行处理)。
statementTypeSTA TEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBatis 使用选择使用 Statement,PreparedStatement 或 CallableStatement。默认值:PREPARED。
resultSetTypeFORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE 中的一种。默认是不设置(驱动自行处理)。
databaseIdIn case there is a configured databaseIdProvider, MyBatis will load all statements with no databaseId attribute or with a databaseId that matches the current one. If case the same statement if found with and without the databaseId the latter will be discarded.

insert, update and delete

数据变更语句 insert,update 和 delete 在它们的实现中非常相似:

<span style="padding: 0px; margin: 0px;"><insert</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"insertAuthor"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Author"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">statementType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"PREPARED"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">keyProperty</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">""</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">keyColumn</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">""</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">useGeneratedKeys</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">""</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">timeout</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"20000"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><update</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"insertAuthor"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Author"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">statementType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"PREPARED"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">timeout</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"20000"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><delete</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"insertAuthor"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Author"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">statementType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"PREPARED"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">timeout</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"20000"</span><span style="padding: 0px; margin: 0px;">></span>
Insert, Update and Delete Attributes
属性描述
id在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType将会传入这条语句的参数类的完全限定名或别名。
parameterMap这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。
flushCache将其设置为 true,不论语句什么时候被带哦用,都会导致缓存被清空。默认值:false。
timeout这个设置驱动程序等待数据库返回请求结果, 并抛出异常时间的最大等待值。默认不设置(驱动自行处理)。
statementTypeSTA TEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBatis 使用选择使用 Statement,PreparedStatement 或 CallableStatement。默认值:PREPARED。
useGeneratedKeys( 仅 对 insert 有 用 ) 这 会 告 诉 MyBatis 使 用 JDBC 的 getGeneratedKeys 方法来取出由数据(比如:像 MySQL 和 SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。
keyProperty(仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。
keyColumn(仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。

下面就是 insert,update 和 delete 语句的示例:

<span style="padding: 0px; margin: 0px;"><insert</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"insertAuthor"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   insert into Author (id,username,password,email,bio)
  values (#{id},#{username},#{password},#{email},#{bio})</span><span style="padding: 0px; margin: 0px;"></insert></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><update</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"updateAuthor"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   update Author set
    username = #{username},
    password = #{password},
    email = #{email},
    bio = #{bio}
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></update></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><delete</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"deleteAuthor"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   delete from Author where id = #{id}</span><span style="padding: 0px; margin: 0px;"></delete></span>

如前所述,插入语句有一点多,它有一些属性和子元素用来处理主键的生成。

首先,如果你的数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server) ,那么你可以设置 useGeneratedKeys=”true”,而且设置 keyProperty 到你已经做好的目标属性上。例如,如果上面的 Author 表已经对 id 使用了自动生成的列类型,那么语句可以修改为:

<span style="padding: 0px; margin: 0px;"><insert</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"insertAuthor"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">useGeneratedKeys</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;">keyProperty</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   insert into Author (username,password,email,bio)
  values (#{username},#{password},#{email},#{bio})</span><span style="padding: 0px; margin: 0px;"></insert></span>

MyBatis 有另外一种方法来处理数据库不支持自动生成类型,或者可能 JDBC 驱动不支持自动生成主键时的主键生成问题。

这里有一个简单(甚至很傻)的示例,它可以生成一个随机 ID(可能你不会这么做, 但是这展示了 MyBatis 处理问题的灵活性,因为它并不真的关心 ID 的生成):

<span style="padding: 0px; margin: 0px;"><insert</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"insertAuthor"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><selectKey</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">keyProperty</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">order</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"BEFORE"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">     select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
  </span><span style="padding: 0px; margin: 0px;"></selectKey></span><span style="padding: 0px; margin: 0px;">   insert into Author
    (id, username, password, email,bio, favourite_section)
  values
    (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})</span><span style="padding: 0px; margin: 0px;"></insert></span>

在上面的示例中,selectKey 元素将会首先运行,Author 的 id 会被设置,然后插入语句会被调用。这给你了一个简单的行为在你的数据库中来处理自动生成的主键, 而不需要使你的 Java 代码变得复杂。

selectKey 元素描述如下:

<span style="padding: 0px; margin: 0px;"><selectKey</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">keyProperty</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">order</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"BEFORE"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">statementType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"PREPARED"</span><span style="padding: 0px; margin: 0px;">></span>
selectKey Attributes
属性描述
keyPropertyselectKey 语句结果应该被设置的目标属性。
resultType结果的类型。MyBatis 通常可以算出来,但是写上也没有问题。 MyBatis 允许任何简单类型用作主键的类型,包括字符串。
order这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先选择主键, 设置 keyProperty 然后执行插入语句。如果设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素- 这和如 Oracle 数据库相似,可以在插入语句中嵌入序列调用。
statementType和前面的相 同,MyBatis 支持 STA TEMENT ,PREPARED 和 CALLABLE 语句的映射类型,分别代表 PreparedStatement 和 CallableStatement 类型。

sql

这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。比如:

<span style="padding: 0px; margin: 0px;"><sql</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"userColumns"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;"> id,username,password </span><span style="padding: 0px; margin: 0px;"></sql></span>

这个 SQL 片段可以被包含在其他语句中,例如:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"hashmap"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select </span><span style="padding: 0px; margin: 0px;"><include</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">refid</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"userColumns"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   from some_table
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

Parameters

在之前的语句中, 你已经看到了一些简单参数的示例。 MyBatis 中参数是非常强大的在元素。对于简单的做法,大概 90%的情况,是不用太多的,比如:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select id, username, password
  from users
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

上面的这个示例说明了一个非常简单的命名参数映射。参数类型被设置为“int” ,因此这个参数可以被设置成任何内容。原生的类型或简单数据类型, 比如整型和没有相关属性的字符串,因此它会完全用参数来替代。然而,如果你传递了一个复杂的对象,那么 MyBatis 的处理方式就会有一点不同。比如:

<span style="padding: 0px; margin: 0px;"><insert</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"insertUser"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   insert into users (id, username, password)
  values (#{id}, #{username}, #{password})</span><span style="padding: 0px; margin: 0px;"></insert></span>

如果 User 类型的参数对象传递到了语句中, username 和 password 属性将会被查找, id、然后它们的值就被传递到预处理语句的参数中。

这点对于传递参数到语句中非常好。但是对于参数映射也有一些其他的特性。

首先,像 MyBatis 的其他部分,参数可以指定一个确定的数据类型。

<span style="padding: 0px; margin: 0px;">#{property,javaType=int,jdbcType=NUMERIC}</span>

像 MyBatis 的剩余部分,javaType 通常可以从参数对象中来去顶,除非对象是一个 HashMap。那么 javaType 应该被确定来保证使用正确类型处理器。

注意 如果 null 被当作值来传递,对于所有可能为空的列,JDBC Type 是需要的。以可以自己通过阅读预处理语句的 setNull()方法的 JavaDocs 文档来研究这个。

为了自定义类型处理器,你可以指定一个确定的类型处理器类(或别名), 比如:

<span style="padding: 0px; margin: 0px;">#{age,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler}</span>

尽管它看起来繁琐,但是实际上是你很少设置它们其中之一。

对于数值类型,对于决定有多少数字是相关的,有一个数值范围。

<span style="padding: 0px; margin: 0px;">#{height,javaType=double,jdbcType=NUMERIC,numericScale=2}</span>

最后,mode 属性允许你指定 IN,OUT 或 INOUT 参数。如果参数为 OUT 或 INOUT, 参数对象属性的真实值将会被改变,就像你期望你需要你个输出参数。如果 mode 为 OUT (或 INOUT) ,而且 jdbcType 为 CURSOR(也就是 Oracle 的 REFCURSOR) ,你必须指定一个 resultMap 来映射结果集到参数类型。要注意这里的 javaType 属性是可选的,如果左边的空白是 jdbcType 的 CURSOR 类型,它会自动地被设置为结果集。

<span style="padding: 0px; margin: 0px;">#{department, mode=OUT, jdbcType=CURSOR, javaType=ResultSet, resultMap=departmentResultMap}</span>

MyBatis 也支持很多高级的数据类型,比如结构体,但是当注册 out 参数时你必须告诉语句类型名称。比如(再次提示,在实际中不要像这样换行):

<span style="padding: 0px; margin: 0px;">#{middleInitial, mode=OUT, jdbcType=STRUCT, jdbcTypeName=MY_TYPE, resultMap=departmentResultMap}</span>

尽管所有这些强大的选项很多时候你只简单指定属性名,MyBatis 会自己计算剩余的。最多的情况是你为 jdbcType 指定可能为空的列名。

<span style="padding: 0px; margin: 0px;">#{firstName}</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">#{middleInitial,jdbcType=VARCHAR}</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">#{lastName}</span>
字符串替换

默认情况下,使用#{}格式的语法会导致 MyBatis 创建预处理语句属性并以它为背景设置安全的值(比如?) 。这样做很安全,很迅速也是首选做法,有时你只是想直接在 SQL 语句中插入一个不改变的字符串。比如,像 ORDER BY,你可以这样来使用:

<span style="padding: 0px; margin: 0px;">ORDER BY $</span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;">columnName</span><span style="padding: 0px; margin: 0px;">}</span>

这里 MyBatis 不会修改或转义字符串。

重要 接受从用户输出的内容并提供给语句中不变的字符串,这样做是不安全的。这会导致潜在的 SQL 注入攻击,因此你不应该允许用户输入这些字段,或者通常自行转义并检查。

Result Maps

resultMap 元素是 MyBatis 中最重要最强大的元素。它就是让你远离 90%的需要从结果集中取出数据的 JDBC 代码的那个东西, 而且在一些情形下允许你做一些 JDBC 不支持的事情。事实上, 编写相似于对复杂语句联合映射这些等同的代码, 也许可以跨过上千行的代码。 ResultMap 的设计就是简单语句不需要明确的结果映射,而很多复杂语句确实需要描述它们的关系。

你已经看到简单映射语句的示例了,但没有明确的 resultMap。比如:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"hashmap"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select id, username, hashedPassword
  from some_table
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

这样一个语句简单作用于所有列被自动映射到 HashMap 的键上,这由 resultType 属性指定。这在很多情况下是有用的,但是 HashMap 不能很好描述一个领域模型。那样你的应用程序将会使用 JavaBeans 或 POJOs(Plain Old Java Objects,普通 Java 对象)来作为领域模型。MyBatis 对两者都支持。看看下面这个 JavaBean:

<span style="padding: 0px; margin: 0px;">package</span><span style="padding: 0px; margin: 0px;"> com</span><span style="padding: 0px; margin: 0px;">.</span><span style="padding: 0px; margin: 0px;">someapp</span><span style="padding: 0px; margin: 0px;">.</span><span style="padding: 0px; margin: 0px;">model</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">class</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">User</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">private</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">int</span><span style="padding: 0px; margin: 0px;"> id</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">  </span><span style="padding: 0px; margin: 0px;">private</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> username</span><span style="padding: 0px; margin: 0px;">;</span>
<span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">private</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> hashedPassword</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;">   
  </span><span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">int</span><span style="padding: 0px; margin: 0px;"> getId</span><span style="padding: 0px; margin: 0px;">()</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;">return</span><span style="padding: 0px; margin: 0px;"> id</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">}</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">  </span><span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">void</span><span style="padding: 0px; margin: 0px;"> setId</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">int</span><span style="padding: 0px; margin: 0px;"> id</span><span style="padding: 0px; margin: 0px;">)</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span>
<span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;">this</span><span style="padding: 0px; margin: 0px;">.</span><span style="padding: 0px; margin: 0px;">id </span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;"> id</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">  </span><span style="padding: 0px; margin: 0px;">}</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">  </span><span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> getUsername</span><span style="padding: 0px; margin: 0px;">()</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;">return</span><span style="padding: 0px; margin: 0px;"> username</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">  </span><span style="padding: 0px; margin: 0px;">}</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">void</span><span style="padding: 0px; margin: 0px;"> setUsername</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> username</span><span style="padding: 0px; margin: 0px;">)</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">this</span><span style="padding: 0px; margin: 0px;">.</span><span style="padding: 0px; margin: 0px;">username </span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;"> username</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">}</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> getHashedPassword</span><span style="padding: 0px; margin: 0px;">()</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;">return</span><span style="padding: 0px; margin: 0px;"> hashedPassword</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;"> </span>
<span style="padding: 0px; margin: 0px;">  </span><span style="padding: 0px; margin: 0px;">}</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">void</span><span style="padding: 0px; margin: 0px;"> setHashedPassword</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> hashedPassword</span><span style="padding: 0px; margin: 0px;">)</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span>
<span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;">this</span><span style="padding: 0px; margin: 0px;">.</span><span style="padding: 0px; margin: 0px;">hashedPassword </span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;"> hashedPassword</span><span style="padding: 0px; margin: 0px;">;</span><span style="padding: 0px; margin: 0px;">  </span>
<span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">}</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">}</span>

基于 JavaBean 的规范,上面这个类有 3 个属性:id,username 和 hashedPassword。这些在 select 语句中会精确匹配到列名。

这样的一个 JavaBean 可以被映射到结果集,就像映射到 HashMap 一样简单。

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"com.someapp.model.User"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select id, username, hashedPassword
  from some_table
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

要记住类型别名是你的伙伴。使用它们你可以不用输入类的全路径。比如:

<span style="padding: 0px; margin: 0px;"><!-- In mybatis-config.xml file --></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><typeAlias</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"com.someapp.model.User"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">alias</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><!-- In SQL Mapping XML file --></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select id, username, hashedPassword
  from some_table
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

这些情况下,MyBatis 会在幕后自动创建一个 ResultMap,基于属性名来映射列到 JavaBean 的属性上。如果列名没有精确匹配,你可以在列名上使用 select 字句的别名(一个基本的 SQL 特性)来匹配标签。比如:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select
    user_id             as "id",
    user_name           as "userName",
    hashed_password     as "hashedPassword"
  from some_table
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

ResultMap 最优秀的地方你已经了解了很多了,但是你还没有真正的看到一个。这些简单的示例不需要比你看到的更多东西。只是出于示例的原因, 让我们来看看最后一个示例中外部的 resultMap 是什么样子的,这也是解决列名不匹配的另外一种方式。

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"userResultMap"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"user_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"password"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"password"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

引用它的语句使用 resultMap 属性就行了(注意我们去掉了 resultType 属性)。比如:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"userResultMap"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select user_id, user_name, hashed_password
  from some_table
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

如果世界总是这么简单就好了。

高级结果映射

MyBatis 创建的一个想法:数据库不用永远是你想要的或需要它们是什么样的。而我们最喜欢的数据库最好是第三范式或 BCNF 模式,但它们有时不是。如果可能有一个单独的数据库映射,所有应用程序都可以使用它,这是非常好的,但有时也不是。结果映射就是 MyBatis 提供处理这个问题的答案。

比如,我们如何映射下面这个语句?

<span style="padding: 0px; margin: 0px;"><!-- Very Complex Statement --></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectBlogDetails"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"detailedBlogResultMap"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   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}</span><span style="padding: 0px; margin: 0px;"></select></span>

你可能想把它映射到一个智能的对象模型,包含一个作者写的博客,有很多的博文,每篇博文有零条或多条的评论和标签。下面是一个完整的复杂结果映射例子 (假设作者, 博客, 博文, 评论和标签都是类型的别名) 我们来看看, 。但是不用紧张, 我们会一步一步来说明。当天最初它看起来令人生畏,但实际上非常简单。

<span style="padding: 0px; margin: 0px;"><!-- Very Complex Result Map --></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"detailedBlogResultMap"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><constructor></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><idArg</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"></constructor></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"title"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_title"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">" Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_username"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"password"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_password"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"email"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_email"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"bio"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_bio"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"favouriteSection"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_favourite_section"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"></association></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"posts"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Post"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"subject"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_subject"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"comments"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">" Comment"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"comment_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"></collection></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"tags"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">" Tag"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"tag_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"></collection></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><discriminator</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"draft"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"1"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"DraftPost"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"></discriminator></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"></collection></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

resultMap 元素有很多子元素和一个值得讨论的结构。下面是 resultMap 元素的概念视图

resultMap
  • constructor - 类在实例化时,用来注入结果到构造方法中
    • idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能
    • arg - 注入到构造方法的一个普通结果
  • id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个复杂的类型关联;许多结果将包成这种类型
    • 嵌入结果映射 – 结果映射自身的关联,或者参考一个
  • collection – 复杂类型的集
    • 嵌入结果映射 – 结果映射自身的集,或者参考一个
  • discriminator – 使用结果值来决定使用哪个结果映射
    • case – 基于某些值的结果映射
      • 嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相 同的元素,或者它可以参照一个外部的结果映射。

最佳实践 通常逐步建立结果映射。单元测试的真正帮助在这里。如果你尝试创建一次创建一个向上面示例那样的巨大的结果映射, 那么可能会有错误而且很难去控制它来工作。开始简单一些,一步一步的发展。而且要进行单元测试!使用该框架的缺点是它们有时是黑盒(是否可见源代码) 。你确定你实现想要的行为的最好选择是编写单元测试。它也可以你帮助得到提交时的错误。

下面一部分将详细说明每个元素。

id & result
<span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"subject"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_subject"</span><span style="padding: 0px; margin: 0px;">/></span>

这些是结果映射最基本内容。id 和 result 都映射一个单独列的值到简单数据类型(字符串,整型,双精度浮点数,日期等)的单独属性或字段。

这两者之间的唯一不同是 id 表示的结果将是当比较对象实例时用到的标识属性。这帮助来改进整体表现,特别是缓存和嵌入结果映射(也就是联合映射) 。

每个都有一些属性:

Id and Result Attributes
属性描述
property映射到列结果的字段或属性。如果匹配的是存在的,和给定名称相同的 JavaBeans 的属性,那么就会使用。否则 MyBatis 将会寻找给定名称 property 的字段。这两种情形你可以使用通常点式的复杂属性导航。比如,你可以这样映射一些东西: “username” ,或者映射到一些复杂的东西: “address.street.number” 。
column从数据库中得到的列名,或者是列名的重命名标签。这也是通常和会传递给 resultSet.getString(columnName)方法参数中相同的字符串。
javaType一个 Java 类的完全限定名,或一个类型别名(参加上面内建类型别名的列表) 。如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。然而,如果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的行为。
jdbcType在这个表格之后的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅仅需要对插入,更新和删除操作可能为空的列进行处理。这是 JDBC jdbcType 的需要,而不是 MyBatis 的。如果你直接使用 JDBC 编程,你需要指定这个类型-但仅仅对可能为空的值。
typeHandler我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理器的实现,或者是类型别名。
支持的 JDBC 类型

为了未来的参考,MyBatis 通过包含的 jdbcType 枚举型,支持下面的 JDBC 类型。

BITFLOATCHARTIMESTAMPOTHERUNDEFINED
TINYINTREALVARCHARBINARYBLOGNVARCHAR
SMALLINTDOUBLELONGVARCHARVARBINARYCLOB>NCHAR
INTEGERNUMERICDATELONGVARBINARYBOOLEANNCLOB
BIGINTDECIMALTIMENULLCURSORARRAY
构造方法
<span style="padding: 0px; margin: 0px;"><constructor></span><span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;"><idArg</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;"><arg</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"String"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></constructor></span>

对于大多数数据传输对象(Data Transfer Object,DTO)类型,属性可以起作用,而且像你绝大多数的领域模型, 指令也许是你想使用一成不变的类的地方。通常包含引用或查询数据的表很少或基本不变的话对一成不变的类来说是合适的。构造方法注入允许你在初始化时为类设置属性的值,而不用暴露出公有方法。MyBatis 也支持私有属性和私有 JavaBeans 属性来达到这个目的,但是一些人更青睐构造方法注入。构造方法元素支持这个。

看看下面这个构造方法:

<span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">class</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">User</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;">//...</span><span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">User</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">int</span><span style="padding: 0px; margin: 0px;"> id</span><span style="padding: 0px; margin: 0px;">,</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> username</span><span style="padding: 0px; margin: 0px;">)</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;">      </span><span style="padding: 0px; margin: 0px;">//...</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">}</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">//...</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">}</span>

为了向这个构造方法中注入结果,MyBatis 需要通过它的参数的类型来标识构造方法。 Java 没有自查(反射)参数名的方法。所以当创建一个构造方法元素时,保证参数是按顺序排列的,而且数据类型也是确定的。

<span style="padding: 0px; margin: 0px;"><constructor></span><span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;"><idArg</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">    </span><span style="padding: 0px; margin: 0px;"><arg</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"String"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></constructor></span>

剩余的属性和规则和固定的 id 和 result 元素是相同的。

属性描述
column来自数据库的类名,或重命名的列标签。这和通常传递给 resultSet.getString(columnName)方法的字符串是相同的。
javaType一个 Java 类的完全限定名,或一个类型别名(参加上面内建类型别名的列表)。如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。然而,如果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的行为。
jdbcType在这个表格之前的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅仅需要对插入, 更新和删除操作可能为空的列进行处理。这是 JDBC 的需要, jdbcType 而不是 MyBatis 的。如果你直接使用 JDBC 编程,你需要指定这个类型-但仅仅对可能为空的值。
typeHandler我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理器的实现, 或者是类型别名。
selectThe ID of another mapped statement that will load the complex type required by this property mapping. The values retrieved from columns specified in the column attribute will be passed to the target select statement as parameters. See the Association element for more.
resultMapThis is the ID of a ResultMap that can map the nested results of this argument into an appropriate object graph. This is an alternative to using a call to another select statement. It allows you to join multiple tables together into a single ResultSet. Such a ResultSet will contain duplicated, repeating groups of data that needs to be decomposed and mapped properly to a nested object graph. To facilitate this, MyBatis lets you "chain" result maps together, to deal with the nested results. See the Association element below for more.
关联
<span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_author_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">" Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_username"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></association></span>

关联元素处理“有一个”类型的关系。比如,在我们的示例中,一个博客有一个用户。关联映射就工作于这种结果之上。你指定了目标属性,来获取值的列,属性的 java 类型(很多情况下 MyBatis 可以自己算出来) ,如果需要的话还有 jdbc 类型,如果你想覆盖或获取的结果值还需要类型控制器。

关联中不同的是你需要告诉 MyBatis 如何加载关联。MyBatis 在这方面会有两种不同的方式:

  • 嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型。
  • 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集。首先,然让我们来查看这个元素的属性。所有的你都会看到,它和普通的只由 select 和

resultMap 属性的结果映射不同。

属性描述
property映射到列结果的字段或属性。如果匹配的是存在的,和给定名称相同的 property JavaBeans 的属性, 那么就会使用。否则 MyBatis 将会寻找给定名称的字段。这两种情形你可以使用通常点式的复杂属性导航。比如,你可以这样映射一 些 东 西 :“ username ”, 或 者 映 射 到 一 些 复 杂 的 东 西 : “address.street.number” 。
javaType一个 Java 类的完全限定名,或一个类型别名(参加上面内建类型别名的列表) 。如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。然而,如 javaType 果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的行为。
jdbcType在这个表格之前的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅仅需要对插入, 更新和删除操作可能为空的列进行处理。这是 JDBC 的需要, jdbcType 而不是 MyBatis 的。如果你直接使用 JDBC 编程,你需要指定这个类型-但仅仅对可能为空的值。
typeHandler我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默认的 typeHandler 类型处理器。这个属性值是类的完全限定名或者是一个类型处理器的实现, 或者是类型别名。
关联的嵌套查询
属性描述
column来自数据库的类名,或重命名的列标签。这和通常传递给 resultSet.getString(columnName)方法的字符串是相同的。 column 注 意 : 要 处 理 复 合 主 键 , 你 可 以 指 定 多 个 列 名 通 过 column= ” {prop1=col1,prop2=col2} ” 这种语法来传递给嵌套查询语 句。这会引起 prop1 和 prop2 以参数对象形式来设置给目标嵌套查询语句。
select另外一个映射语句的 ID,可以加载这个属性映射需要的复杂类型。获取的在列属性中指定的列的值将被传递给目标 select 语句作为参数。表格后面有一个详细的示例。 select 注 意 : 要 处 理 复 合 主 键 , 你 可 以 指 定 多 个 列 名 通 过 column= ” {prop1=col1,prop2=col2} ” 这种语法来传递给嵌套查询语 句。这会引起 prop1 和 prop2 以参数对象形式来设置给目标嵌套查询语句。

示例:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_author_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">select</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectAuthor"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectBlog"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   SELECT * FROM BLOG WHERE ID = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectAuthor"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   SELECT * FROM AUTHOR WHERE ID = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

我们有两个查询语句:一个来加载博客,另外一个来加载作者,而且博客的结果映射描述了“selectAuthor”语句应该被用来加载它的 author 属性。

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

这种方式很简单, 但是对于大型数据集合和列表将不会表现很好。问题就是我们熟知的 “N+1 查询问题”。概括地讲,N+1 查询问题可以是这样引起的:

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

这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。

MyBatis 能延迟加载这样的查询就是一个好处,因此你可以分散这些语句同时运行的消耗。然而,如果你加载一个列表,之后迅速迭代来访问嵌套的数据,你会调用所有的延迟加载,这样的行为可能是很糟糕的。

所以还有另外一种方法。

关联的嵌套结果
属性描述
resultMap这是结果映射的 ID,可以映射关联的嵌套结果到一个合适的对象图中。这是一种替代方法来调用另外一个查询语句。这允许你联合多个表来合成到 resultMap 一个单独的结果集。这样的结果集可能包含重复,数据的重复组需要被分解,合理映射到一个嵌套的对象图。为了使它变得容易,MyBatis 让你“链接”结果映射,来处理嵌套结果。一个例子会很容易来仿照,这个表格后面也有一个示例。
columnPrefixWhen joining multiple tables, you would have to use column alias to avoid duplicated column names in the ResultSet. Specifying columnPrefix allows you to map such columns to an external resultMap. Please see the example explained later in this section.

在上面你已经看到了一个非常复杂的嵌套关联的示例。下面这个是一个非常简单的示例来说明它如何工作。代替了执行一个分离的语句,我们联合博客表和作者表在一起,就像:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectBlog"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   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}</span><span style="padding: 0px; margin: 0px;"></select></span>

注意这个联合查询, 以及采取保护来确保所有结果被唯一而且清晰的名字来重命名。这使得映射非常简单。现在我们可以映射这个结果:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"title"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_title"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_author_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"authorResult"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"authorResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_username"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"password"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_password"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"email"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_email"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"bio"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_bio"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

在上面的示例中你可以看到博客的作者关联代表着“authorResult”结果映射来加载作者实例。

非常重要: 在嵌套据诶过映射中 id 元素扮演了非常重要的角色。应应该通常指定一个或多个属性,它们可以用来唯一标识结果。实际上就是如果你离开她了,但是有一个严重的性能问题时 MyBatis 仍然可以工作。选择的属性越少越好,它们可以唯一地标识结果。主键就是一个显而易见的选择(尽管是联合主键)。

现在,上面的示例用了外部的结果映射元素来映射关联。这使得 Author 结果映射可以重用。然而,如果你不需要重用它的话,或者你仅仅引用你所有的结果映射合到一个单独描述的结果映射中。你可以嵌套结果映射。这里给出使用这种方式的相同示例:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"title"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_title"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_username"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"password"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_password"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"email"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_email"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"bio"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_bio"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"></association></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

What if the blog has a co-author? The select statement would look like:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectBlog"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   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}</span><span style="padding: 0px; margin: 0px;"></select></span>

Recall that the resultMap for Author is defined as follows.

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"authorResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_username"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"password"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_password"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"email"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_email"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"bio"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_bio"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

Because the column names in the results differ from the columns defined in the resultMap, you need to specify columnPrefix to reuse the resultMap for mapping co-author results.

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"title"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_title"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"authorResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"coAuthor"</span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"authorResult"</span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;">columnPrefix</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"co_"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

上面你已经看到了如何处理“有一个”类型关联。但是“有很多个”是怎样的?下面这个部分就是来讨论这个主题的。

集合
<span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"posts"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"domain.blog.Post"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"subject"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_subject"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"body"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_body"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></collection></span>

集合元素的作用几乎和关联是相同的。实际上,它们也很相似,文档的异同是多余的。所以我们更多关注于它们的不同。

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

<span style="padding: 0px; margin: 0px;">private</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">List</span><span style="padding: 0px; margin: 0px;"><</span><span style="padding: 0px; margin: 0px;">Post</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;"> posts</span><span style="padding: 0px; margin: 0px;">;</span>

要映射嵌套结果集合到 List 中,我们使用集合元素。就像关联元素一样,我们可以从连接中使用嵌套查询,或者嵌套结果。

集合的嵌套查询

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

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"posts"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"ArrayList"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Post"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">select</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectPostsForBlog"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectBlog"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   SELECT * FROM BLOG WHERE ID = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectPostsForBlog"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   SELECT * FROM POST WHERE BLOG_ID = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>

这里你应该注意很多东西,但大部分代码和上面的关联元素是非常相似的。首先,你应该注意我们使用的是集合元素。然后要注意那个新的“ofType”属性。这个属性用来区分 JavaBean(或字段)属性类型和集合包含的类型来说是很重要的。所以你可以读出下面这个映射:

<span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"posts"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"ArrayList"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Post"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">select</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectPostsForBlog"</span><span style="padding: 0px; margin: 0px;">/></span>

读作: “在 Post 类型的 ArrayList 中的 posts 的集合。”

javaType 属性是不需要的,因为 MyBatis 在很多情况下会为你算出来。所以你可以缩短写法:

<span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"posts"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Post"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">select</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectPostsForBlog"</span><span style="padding: 0px; margin: 0px;">/></span>
集合的嵌套结果

至此,你可以猜测集合的嵌套结果是如何来工作的,因为它和关联完全相同,除了它应用了一个“ofType”属性

First, let's look at the SQL:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectBlog"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   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}</span><span style="padding: 0px; margin: 0px;"></select></span>

我们又一次联合了博客表和文章表,而且关注于保证特性,结果列标签的简单映射。现在用文章映射集合映射博客,可以简单写为:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"title"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_title"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"posts"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Post"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"subject"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_subject"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"body"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_body"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"></collection></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

同样,要记得 id 元素的重要性,如果你不记得了,请阅读上面的关联部分。

同样, 如果你引用更长的形式允许你的结果映射的更多重用, 你可以使用下面这个替代的映射:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"title"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blog_title"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><collection</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"posts"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">ofType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Post"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogPostResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">columnPrefix</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"post_"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogPostResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Post"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"subject"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"subject"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"body"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"body"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

注意 这个对你所映射的内容没有深度,广度或关联和集合相联合的限制。当映射它们时你应该在大脑中保留它们的表现。你的应用在找到最佳方法前要一直进行的单元测试和性能测试。好在 myBatis 让你后来可以改变想法,而不对你的代码造成很小(或任何)影响。

高级关联和集合映射是一个深度的主题。文档只能给你介绍到这了。加上一点联系,你会很快清楚它们的用法。

鉴别器
<span style="padding: 0px; margin: 0px;"><discriminator</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"draft"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"1"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"DraftPost"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></discriminator></span>

有时一个单独的数据库查询也许返回很多不同 (但是希望有些关联) 数据类型的结果集。鉴别器元素就是被设计来处理这个情况的, 还有包括类的继承层次结构。鉴别器非常容易理解,因为它的表现很像 Java 语言中的 switch 语句。

定义鉴别器指定了 column 和 javaType 属性。列是 MyBatis 查找比较值的地方。 JavaType 是需要被用来保证等价测试的合适类型(尽管字符串在很多情形下都会有用)。比如:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vehicleResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Vehicle"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vin"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vin"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"year"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"year"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"make"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"make"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"model"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"model"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"color"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"color"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><discriminator</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vehicle_type"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"1"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"carResult"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"2"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"truckResult"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"3"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vanResult"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"4"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"suvResult"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"></discriminator></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

在这个示例中, MyBatis 会从结果集中得到每条记录, 然后比较它的 vehicle 类型的值。如果它匹配任何一个鉴别器的实例,那么就使用这个实例指定的结果映射。换句话说,这样做完全是剩余的结果映射被忽略(除非它被扩展,这在第二个示例中讨论) 。如果没有任何一个实例相匹配,那么 MyBatis 仅仅使用鉴别器块外定义的结果映射。所以,如果 carResult 按如下声明:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"carResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Car"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"doorCount"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"door_count"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

那么只有 doorCount 属性会被加载。这步完成后完整地允许鉴别器实例的独立组,尽管和父结果映射可能没有什么关系。这种情况下,我们当然知道 cars 和 vehicles 之间有关系, 如 Car 是一个 Vehicle 实例。因此,我们想要剩余的属性也被加载。我们设置的结果映射的简单改变如下。

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"carResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Car"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">extends</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vehicleResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"doorCount"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"door_count"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

现在 vehicleResult 和 carResult 的属性都会被加载了。

尽管曾经有些人会发现这个外部映射定义会多少有一些令人厌烦之处。因此还有另外一种语法来做简洁的映射风格。比如:

<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vehicleResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Vehicle"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><id</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"id"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vin"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vin"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"year"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"year"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"make"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"make"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"model"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"model"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"color"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"color"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><discriminator</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vehicle_type"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"1"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"carResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"doorCount"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"door_count"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"></case></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"2"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"truckResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"boxSize"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"box_size"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"extendedCab"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"extended_cab"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"></case></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"3"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"vanResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"powerSlidingDoor"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"power_sliding_door"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"></case></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"><case</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"4"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"suvResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">       </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"allWheelDrive"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"all_wheel_drive"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;">     </span><span style="padding: 0px; margin: 0px;"></case></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"></discriminator></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

要记得 这些都是结果映射, 如果你不指定任何结果, 那么 MyBatis 将会为你自动匹配列和属性。所以这些例子中的大部分是很冗长的,而其实是不需要的。也就是说,很多数据库是很复杂的,我们不太可能对所有示例都能依靠它。

Auto-mapping

As you have already seen in the previous sections, in simple cases MyBatis can auto-map the results for you and in others you will need to build a result map. But as you will see in this section you can also mix both strategies. Let's have a deeper look at how auto-mapping works.

When auto-mapping results MyBatis will get the column name and look for a property with the same name ignoring case. That means that if a column named ID and property named id are found, MyBatis will set the id property with the ID column value.

Usually database columns are named using uppercase letters and underscores between words and java properties often follow the camelcase naming covention. To enable the auto-mapping between them set the setting mapUnderscoreToCamelCase to true.

Auto-mapping works even when there is an specific result map. When this happens, for each result map, all columns that are present in the ResultSet that have not a manual mapping will be auto-mapped, then manual mappings will be processed. In the following sample id and userName columns will be auto-mapped and hashed_password column will be mapped.

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectUsers"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select
    user_id             as "id",
    user_name           as "userName",
    hashed_password
  from some_table
  where id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>
<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"userResultMap"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"User"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"password"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"hashed_password"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

There are three auto-mapping levels:

  • NONE - disables auto-mapping. Only manually mapped properties will be set.
  • PARTIAL - will auto-map results except those that have nested result mappings defined inside (joins).
  • FULL - auto-maps everything.

The default value is PARTIAL, and it is so for a reason. When FULL is used auto-mapping will be performed when processing join results and joins retrieve data of several different entities in the same row hence this may result in undesired mappings. To understand the risk have a look at the following sample:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"selectBlog"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">parameterType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"int"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   select
    B.id,
    B.title,
    A.username,
  from Blog B left outer join Author A on B.author_id = A.id
  where B.id = #{id}</span><span style="padding: 0px; margin: 0px;"></select></span>
<span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"blogResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Blog"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><association</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">javaType</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">resultMap</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"authorResult"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><resultMap</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">id</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"authorResult"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"Author"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><result</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">property</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"username"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">column</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"author_username"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></resultMap></span>

With this result map both Blog and Author will be auto-mapped. But note that Author has an idproperty and there is a column named id in the ResultSet so Author's id will be filled with Blog's id, and that is not what you were expecting. So use the FULL option with caution.

缓存

MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制。MyBatis 3 中的缓存实现的很多改进都已经实现了,使得它更加强大而且易于配置。

默认情况下是没有开启缓存的,除了局部的 session 缓存,可以增强变现而且处理循环依赖也是必须的。要开启二级缓存,你需要在你的 SQL 映射文件中添加一行:

<span style="padding: 0px; margin: 0px;"><cache/></span>

字面上看就是这样。这个简单语句的效果如下:

  • 映射语句文件中的所有 select 语句将会被缓存。
  • 映射语句文件中的所有 insert,update 和 delete 语句会刷新缓存。
  • 缓存会使用 Least Recently Used(LRU,最近最少使用的)算法来收回。
  • 根据时间表(比如 no Flush Interval,没有刷新间隔), 缓存不会以任何时间顺序 来刷新。
  • 缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用。
  • 缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而且可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。

所有的这些属性都可以通过缓存元素的属性来修改。比如:

<span style="padding: 0px; margin: 0px;"><cache</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">eviction</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"FIFO"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">flushInterval</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"60000"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">size</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"512"</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">readOnly</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">/></span>

这个更高级的配置创建了一个 FIFO 缓存,并每隔 60 秒刷新,存数结果对象或列表的 512 个引用,而且返回的对象被认为是只读的,因此在不同线程中的调用者之间修改它们会导致冲突。

可用的收回策略有:

  • LRU – 最近最少使用的:移除最长时间不被使用的对象。
  • FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
  • SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
  • WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。

默认的是 LRU。

flushInterval(刷新间隔)可以被设置为任意的正整数,而且它们代表一个合理的毫秒形式的时间段。默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。

size(引用数目)可以被设置为任意正整数,要记住你缓存的对象数目和你运行环境的可用内存资源数目。默认值是 1024。

readOnly(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。可读写的缓存会返回缓存对象的拷贝(通过序列化) 。这会慢一些,但是安全,因此默认是 false。

使用自定义缓存

除了这些自定义缓存的方式, 你也可以通过实现你自己的缓存或为其他第三方缓存方案创建适配器来完全覆盖缓存行为。

<span style="padding: 0px; margin: 0px;"><cache</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"com.domain.something.MyCustomCache"</span><span style="padding: 0px; margin: 0px;">/></span>

这个示 例展 示了 如何 使用 一个 自定义 的缓 存实 现。type 属 性指 定的 类必 须实现 org.mybatis.cache.Cache 接口。这个接口是 MyBatis 框架中很多复杂的接口之一,但是简单给定它做什么就行。

<span style="padding: 0px; margin: 0px;">public</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">interface</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">Cache</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">{</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">String</span><span style="padding: 0px; margin: 0px;"> getId</span><span style="padding: 0px; margin: 0px;">();</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">int</span><span style="padding: 0px; margin: 0px;"> getSize</span><span style="padding: 0px; margin: 0px;">();</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">void</span><span style="padding: 0px; margin: 0px;"> putObject</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">Object</span><span style="padding: 0px; margin: 0px;"> key</span><span style="padding: 0px; margin: 0px;">,</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">Object</span><span style="padding: 0px; margin: 0px;"> value</span><span style="padding: 0px; margin: 0px;">);</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">Object</span><span style="padding: 0px; margin: 0px;"> getObject</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">Object</span><span style="padding: 0px; margin: 0px;"> key</span><span style="padding: 0px; margin: 0px;">);</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">boolean</span><span style="padding: 0px; margin: 0px;"> hasKey</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">Object</span><span style="padding: 0px; margin: 0px;"> key</span><span style="padding: 0px; margin: 0px;">);</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">Object</span><span style="padding: 0px; margin: 0px;"> removeObject</span><span style="padding: 0px; margin: 0px;">(</span><span style="padding: 0px; margin: 0px;">Object</span><span style="padding: 0px; margin: 0px;"> key</span><span style="padding: 0px; margin: 0px;">);</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">void</span><span style="padding: 0px; margin: 0px;"> clear</span><span style="padding: 0px; margin: 0px;">();</span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;">ReadWriteLock</span><span style="padding: 0px; margin: 0px;"> getReadWriteLock</span><span style="padding: 0px; margin: 0px;">();</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">}</span>

要配置你的缓存, 简单和公有的 JavaBeans 属性来配置你的缓存实现, 而且是通过 cache 元素来传递属性, 比如, 下面代码会在你的缓存实现中调用一个称为 “setCacheFile(String file)” 的方法:

<span style="padding: 0px; margin: 0px;"><cache</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">type</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"com.domain.something.MyCustomCache"</span><span style="padding: 0px; margin: 0px;">></span><span style="padding: 0px; margin: 0px;">   </span><span style="padding: 0px; margin: 0px;"><property</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">name</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"cacheFile"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">value</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"/tmp/my-custom-cache.tmp"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"></cache></span>

你可以使用所有简单类型作为 JavaBeans 的属性,MyBatis 会进行转换。

记得缓存配置和缓存实例是绑定在 SQL 映射文件的命名空间是很重要的。因此,所有在相同命名空间的语句正如绑定的缓存一样。语句可以修改和缓存交互的方式, 或在语句的语句的基础上使用两种简单的属性来完全排除它们。默认情况下,语句可以这样来配置:

<span style="padding: 0px; margin: 0px;"><select</span><span style="padding: 0px; margin: 0px;"> ... </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"false"</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">useCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><insert</span><span style="padding: 0px; margin: 0px;"> ... </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><update</span><span style="padding: 0px; margin: 0px;"> ... </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">/></span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;"><delete</span><span style="padding: 0px; margin: 0px;"> ... </span><span style="padding: 0px; margin: 0px;">flushCache</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"true"</span><span style="padding: 0px; margin: 0px;">/></span>

因为那些是默认的,你明显不能明确地以这种方式来配置一条语句。相反,如果你想改变默认的行为,只能设置 flushCache 和 useCache 属性。比如,在一些情况下你也许想排除从缓存中查询特定语句结果,或者你也许想要一个查询语句来刷新缓存。相似地,你也许有一些更新语句依靠执行而不需要刷新缓存。

参照缓存

回想一下上一节内容, 这个特殊命名空间的唯一缓存会被使用或者刷新相同命名空间内的语句。也许将来的某个时候,你会想在命名空间中共享相同的缓存配置和实例。在这样的情况下你可以使用 cache-ref 元素来引用另外一个缓存。

<span style="padding: 0px; margin: 0px;"><cache-ref</span><span style="padding: 0px; margin: 0px;"> </span><span style="padding: 0px; margin: 0px;">namespace</span><span style="padding: 0px; margin: 0px;">=</span><span style="padding: 0px; margin: 0px;">"com.someone.application.data.SomeMapper"</span><span style="padding: 0px; margin: 0px;">/></span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值