1 Configuration Structure
- properties
- settings
- typeAliases
- typeHandlers
- objectFactory
- plugins
-
environments
- environment
- transactionManager
- dataSource
- environment
- databaseIdProvider
- mappers
2 properties
2.1 如何定义?
These are externalizable, substitutable properties that can be configured in a typical Java Properties file instance, or passed in through sub-elements of the properties element.
这些属性是可以外部配置、可替换的,可以在典型的 Java 属性文件中配置,或者通过 properties 元素的子元素进行配置。
- 在 Configuration XML 中定义
<properties resource="org/mybatis/example/config.properties">
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>
- 在 config.properties 中定义
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
- 在 Java 中定义
Properties prop = new Properties();
prop.setProperty("key", "value");
2.2 在哪里使用?
- 在 XML 中使用
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
The username and password in this example will be replaced by the values set in the properties elements. The driver and url properties would be replaced with values contained from the config.properties file. This provides a lot of options for configuration.
在这个例子中,username 和 password 将被 properties 元素中的设置的对应值所替换。driver 和 url 将被 config.properties 文件中设置的值替换。这样就为 configuration 提供了诸多选择。
- 在 Java 类中使用
Properties can also be passed into the SqlSessionFactoryBuilder.build() methods.
Properties 也可以在 SqlSessionFactoryBuilder.build() 方法中使用:
SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, props);
// ... or ...
SqlSessionFactory factory = new SqlSessionFactoryBuilder.build(reader, environment, props);
2.3 顺序?
If a property exists in more than one of these places, MyBatis loads them in the following order:
如果一个属性在多个地方被定义,Mybatis 将按以下顺序加载:
Properties specified in the body of the properties element are read first,
定义在 properties 元素体内的属性将被优先读取;Properties loaded from the classpath resource or url attributes of the properties element are read second, and override any duplicate properties already specified,
然后根据 properties 的 resource 属性读取类路径属性文件或者通过 properties 元素的 url 属性指定的路径读取属性文件,并且覆盖已读取的同名属性;Properties passed as a method parameter are read last, and override any duplicate properties that may have been loaded from the properties body and the resource/url attributes.
最后读取通过方法参数传递的属性,并且覆盖掉以上两种方式加载的同名属性。
Thus, the highest priority properties are those passed in as a method parameter, followed by resource/url attributes and finally the properties specified in the body of the properties element.
因此,优先级顺序从高到低依次是:通过方法参数传递的属性-->通过 resource/url 的路径下文件定义的属性-->properties 元素体中定义的属性。
2.4 更多关于占位符
Since the MyBatis 3.4.2, your can specify a default value into placeholder as follow:
从 MyBatis 3.4.2 开始,你可以为占位符指定一个默认值:
<dataSource type="POOLED">
<!-- ... -->
<!-- 如果 'username' 属性未设置, username 则为默认值 'ut_user' -->
<property name="username" value="${username:ut_user}"/>
</dataSource>
This feature is disabled by default. If you specify a default value into placeholder, you should be enable this feature by adding a special property as follow:
开启特性:这个特性默认是关闭的。如果你想为占位符指定一个默认值,你应该加入下面这样一个 property 来打开它:
<properties resource="org/mybatis/example/config.properties">
<!-- ... -->
<!-- 开启该特性 -->
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
</properties>
Also If you are used already the ":" as property key(e.g. db:username) or you are used already the ternary operator of OGNL expression(e.g. ${tableName != null ? tableName : 'global_constants'}) on your sql definition, you should be change the character that separate key and default value by adding a special property as follow:
自定义分隔符:如果你的属性键中已经包含了字符":"(如 db:username)或者你已经在你的 sql 定义中用了 OGNL表达式的三元运算符(如 &{tableName != null ? tableName : 'global_constants'}),你应该通过增加一个指定的 property 来改变分隔键和默认值的字符:
<properties resource="org/mybatis/example/config.properties">
<!-- ... -->
<!-- 改变默认的分隔符(使用 "?:" 来分隔)-->
<property name="org.apache.ibatis.parsing.PropertyParser.default-value-separator" value="?:"/>
</properties>
然后你就可以像下面这样使用:
<dataSource type="POOLED">
<!-- ... -->
<property name="username" value="${db:username?:ut_user}"/>
</dataSource>
最后
说明:MyBatis 官网提供了简体中文的翻译,但个人觉得较为生硬,甚至有些地方逻辑不通,于是自己一个个重新敲着翻译的(都不知道哪里来的自信...),有些地方同官网翻译有出入,有些倔强地保留了自己的,有的实在别扭则保留了官网的,这些都会在实践中一一更正。鉴于个人英文能力有限,文章中保留了官方文档原英文介绍(个别地方加以调整修剪),希望有缘看到这里的朋友们能够有自己的理解,不会被我可能错误或不合理的翻译带跑偏(〃'▽'〃),欢迎指正!
当前版本:mybatis-3.5.0
官网文档:MyBatis
官网翻译:MyBatis 简体中文
项目实践:MyBatis Learn