1 Configuration Structure
- properties
- settings
- typeAliases
- typeHandlers
- objectFactory
- plugins
-
environments
- environment
- transactionManager
- dataSource
- environment
- databaseIdProvider
- mappers
2 DatabaseIdProvider
2.1 默認(rèn)的 DatabaseIdProvider
MyBatis is able to execute different statements depending on your database vendor.
作用:MyBatis 可以根據(jù)不同的數(shù)據(jù)庫(kù)廠商執(zhí)行不同的語(yǔ)句。
The multi-db vendor support is based on the mapped statements databaseId attribute. MyBatis will load all statements with no databaseId attribute or with a databaseId that matches the current one.In case the same statement is found with and without the databaseId the latter will be discarded.
這種多數(shù)據(jù)廠商的支持是基于映射語(yǔ)句的 databaseId 屬性锦援。MyBatis 會(huì)加載不帶 databaseId 屬性或者帶有當(dāng)前數(shù)據(jù)庫(kù)的 databaseId 的所有語(yǔ)句猛蔽。如果同時(shí)找到帶 databaseId 和不帶 databaseId 的語(yǔ)句,后者會(huì)被舍棄灵寺。
To enable the multi vendor support add a databaseIdProvider to mybatis-config.xml file as follows:
可以在 mybatis-config.xml 中添加 databaseIdProvider 以打開(kāi)對(duì)多數(shù)據(jù)廠商的支持:
<databaseIdProvider type="DB_VENDOR" />
The DB_VENDOR implementation databaseIdProvider sets as databaseId the String returned by DatabaseMetaData#getDatabaseProductName().
DB_VENDOR 對(duì)于 databaseIdProvider 的實(shí)現(xiàn)會(huì)通過(guò) DatabaseMetaData#getDatabaseProductName() 返回的字符串來(lái)設(shè)置 databaseId曼库。
Given that usually that string is too long and that different versions of the same product may return different values, you may want to convert it to a shorter one by adding properties like follows:
由于通常情況下這個(gè)字符串會(huì)很長(zhǎng)并且同一個(gè)產(chǎn)品的的不同版本會(huì)返回不同的值,你可能會(huì)想要像下面這樣通過(guò)添加一些屬性的方式讓它變短:
<databaseIdProvider type="DB_VENDOR">
<property name="SQL Server" value="sqlserver"/>
<property name="DB2" value="db2"/>
<property name="Oracle" value="oracle" />
</databaseIdProvider>
When properties are provided, the DB_VENDOR databaseIdProvider will search the property value corresponding to the first key found in the returned database product name or "null" if there is not a matching property.In this case, if getDatabaseProductName() returns "Oracle (DataDirect)" the databaseId will be set to "oracle".
當(dāng)提供這些屬性時(shí)略板, DB_VENDOR databaseIdProvider 將會(huì)查找對(duì)應(yīng)于返回的數(shù)據(jù)庫(kù)產(chǎn)品名稱的第一個(gè)鍵的屬性值毁枯,如果沒(méi)有匹配的屬性值將會(huì)被設(shè)置為 null。在這個(gè)例子中叮称,如果 getDatabaseProductName() 返回的是“Oracle (DataDirect)”种玛,databaseId 將會(huì)被設(shè)置為 “oracle”。
2.2 自定義 DatabaseIdProvider
You can build your own DatabaseIdProvider by implementing the interface org.apache.ibatis.mapping.DatabaseIdProvider and registering it in mybatis-config.xml:
你可以通過(guò)實(shí)現(xiàn)接口 org.apache.ibatis.mapping.DatabaseIdProvider 并在 mybatis-config.xml 中注冊(cè)的方式創(chuàng)建你自己的 DatabaseIdProvider瓤檐。
public interface MyDatabaseIdProvider {
void setProperties(Properties p);
String getDatabaseId(DataSource dataSource) throws SQLException;
}
具體如下:
1.實(shí)現(xiàn) DatabaseIdProvider 接口:
public class MyDatabaseIdProvider implements DatabaseIdProvider {
Logger logger = Logger.getLogger(MyDatabaseIdProvider.class);
private Properties properties;
@Override
public void setProperties(Properties p) {
this.properties = p;
}
@Override
public String getDatabaseId(DataSource dataSource) throws SQLException {
Connection connection = dataSource.getConnection();
DatabaseMetaData metaData = connection.getMetaData();
String databaseProductName = metaData.getDatabaseProductName();
logger.info("MyDatabaseIdProvider-Current DataBase Product Name is: " + databaseProductName);
for (Object key : properties.keySet()) {
if (key.equals(databaseProductName)) {
logger.info("MyDatabaseIdProvider-Find a matched property value: " + properties.get(key));
return (String) properties.get(key);
}
}
return null;
}
}
2.mybatis-config.xml 中配置多個(gè) environment蒂誉,并指定自定義 databaseIdProvider :
<environments default="developmentOracle">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
<environment id="developmentOracle">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${oracle.driver}"/>
<property name="url" value="${oracle.url}"/>
<property name="username" value="${oracle.username}"/>
<property name="password" value="${oracle.password}"/>
</dataSource>
</environment>
</environments>
<!--自定義 databaseIdProvider-->
<databaseIdProvider type="com.zhaoxueer.learn.my.MyDatabaseIdProvider">
<property name="Oracle" value="oracle"/>
<property name="MySQL" value="mysql"/>
</databaseIdProvider>
3.Mapper.xml 中的語(yǔ)句要指定 databaseId:
<select id="selectByPhone" resultType="Author" databaseId="mysql">
select
id, name, sex, phone
from author
where phone = #{phone}
</select>
<select id="selectByPhone" resultType="Author" databaseId="oracle">
select
id, name, sex
from author
where phone = #{phone}
</select>
通過(guò)上面的配置和代碼編寫(xiě),MyBatis 會(huì)執(zhí)行 databaseId="oracle" 的語(yǔ)句距帅。
最后
說(shuō)明:MyBatis 官網(wǎng)提供了簡(jiǎn)體中文的翻譯右锨,但個(gè)人覺(jué)得較為生硬,甚至有些地方邏輯不通碌秸,于是自己一個(gè)個(gè)重新敲著翻譯的(都不知道哪里來(lái)的自信...)绍移,有些地方同官網(wǎng)翻譯有出入,有些倔強(qiáng)地保留了自己的讥电,有的實(shí)在別扭則保留了官網(wǎng)的蹂窖,這些都會(huì)在實(shí)踐中一一更正。鑒于個(gè)人英文能力有限恩敌,文章中保留了官方文檔原英文介紹(個(gè)別地方加以調(diào)整修剪)瞬测,希望有緣看到這里的朋友們能夠有自己的理解,不會(huì)被我可能錯(cuò)誤或不合理的翻譯帶跑偏(〃'▽'〃)纠炮,歡迎指正月趟!
當(dāng)前版本:mybatis-3.5.0
官網(wǎng)文檔:MyBatis
官網(wǎng)翻譯:MyBatis 簡(jiǎn)體中文
項(xiàng)目實(shí)踐:MyBatis Learn