1 Mapper XML
- select
- insert, update and delete
- sql
- Parameters
- Result Maps
- Auto-mapping
- cache
2 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.
在简单的场景下,MyBatis 可以为你自动映射结果。其他情况下,你需要构建 resultMap。但是在本节中你也可以混合使用这两种策略。让我们来看看自动映射是如何工作的。
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.
当自动映射结果时,MyBatis 将获取列名并查找有相同名称的属性(忽略大小写)。这意味着如果一个列名为 ID,同时有一个名为 id 的属性被找到,MyBatis 会将 id 属性设置为 ID 列对应的值。
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.
通常数据库的列被命名为大写字母并用下划线分隔两个单词,而 java 属性经常遵循驼峰命名规则。为了在这两者之间开启自动映射,设置 mapUnderscoreToCamelCase 为 true(Configuration XML 中)。
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.
即使指定了一个 resultMap,自动映射依然能正常工作。这种情况下,对于每一个 resultMap,结果集中所有没有手动匹配的列都会被自动映射,然后手动映射才会被处理。
In the following sample id and userName columns will be auto-mapped and hashed_password column will be mapped.
在下面这个例子中 id 和 userName 列将会被自动映射,hashed_password 列会根据配置映射。
<select id="selectUsers" resultMap="userResultMap">
select
user_id as "id",
user_name as "userName",
hashed_password
from some_table
where id = #{id}
</select>
<resultMap id="userResultMap" type="User">
<result property="password" column="hashed_password"/>
</resultMap>
自动映射级别:
NONE - disables auto-mapping. Only manually mapped properties will be set.
NONE:禁用自动映射。只有手动映射属性才会被设置。PARTIAL - will auto-map results except those that have nested result mappings defined inside (joins).
PARTIAL:将会自动映射除了内部定义的包含嵌套结果映射的结果(join)。FULL - auto-maps everything.
FULL:自动映射所有。
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:
默认值为 PARTIAL,这是有原因的。当设置为 FULL 时,自动映射会在处理连接查询结果时执行,并且连接获取同一行的几个不同实体的数据,因此这将会导致不可预期的映射。为了理解这个风险我们可以看下面这个例子:
<select id="selectBlog" resultMap="blogResult">
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}
</select>
<resultMap id="blogResult" type="Blog">
<association property="author" resultMap="authorResult"/>
</resultMap>
<resultMap id="authorResult" type="Author">
<result property="username" column="author_username"/>
</resultMap>
With this result map both Blog and Author will be auto-mapped. But note that Author has an id property 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.
在这个结果映射中,Blog 和 Author 都会被自动映射。但是要注意 Author 有一个 id 属性,而结果集中也有一个 id 属性,因此 Author 的 id 将会被填充为 Blog 的 id。这是你所不希望看到的。所以使用 FULL 选项要当心。
Regardless of the auto-mapping level configured you can enable or disable the automapping for an specific ResultMap by adding the attribute autoMapping to it:
通过添加 autoMapping 属性,你可以忽略自动映射级别配置并开启或关闭一个指定 ResultMap 的自动映射:
<resultMap id="userResultMap" type="User" autoMapping="false">
<result property="password" column="hashed_password"/>
</resultMap>
最后
说明:MyBatis 官网提供了简体中文的翻译,但个人觉得较为生硬,甚至有些地方逻辑不通,于是自己一个个重新敲着翻译的(都不知道哪里来的自信...),有些地方同官网翻译有出入,有些倔强地保留了自己的,有的实在别扭则保留了官网的,这些都会在实践中一一更正。鉴于个人英文能力有限,文章中保留了官方文档原英文介绍(个别地方加以调整修剪),希望有缘看到这里的朋友们能够有自己的理解,不会被我可能错误或不合理的翻译带跑偏(〃'▽'〃),欢迎指正!
当前版本:mybatis-3.5.0
官网文档:MyBatis
官网翻译:MyBatis 简体中文
项目实践:MyBatis Learn