MyBatis 的核心配置檔案是 mybatis-config.xml。注意配置檔案中節點的順序有要求,必須按照下面的順序填寫節點資訊:
(properties,settings,typeAliases,typeHandlers,objectFactory,objectWrapperFactory,reflectorFactory,plugins,environments,databaseIdProvider,mappers)
<configuration>
<properties></properties>
<settings></settings>
<typeAliases></typeAliases>
<typeHandlers></typeHandlers>
<objectFactory></objectFactory>
<objectWrapperFactory></objectWrapperFactory>
<reflectorFactory></reflectorFactory>
<plugins></plugins>
<environments></environments>
<databaseIdProvider></databaseIdProvider>
<mappers></mappers>
</configuration>
環境配置與 mappers 映射器
基本的 MyBatis 配置:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!--每個mapper.xml都需要在mybatis配置檔案中進行配置-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
在上面這段配置中配置了運作環境以及 mappers 映射器。
配置 environment 時,通過 transactionManager 指定事務管理器,通過 dataSource 指定資料庫的引擎類型、連接配接方式、使用者名、密碼。
mappers 映射器用來和代碼中寫的 mapper 一一對應,在代碼中寫一個 mapper 接口和 mapper.xml 檔案,就需要在 mappers 映射器中增加一個 mapper 節點。
屬性(properties)
在寫 mybatis-config.xml 環境配置的時候,将資料庫的連接配接資訊直接寫在了 mybatis-config.xml 配置檔案中,不友善後續的更改,是以可以使用屬性(properties)的能力。
在 resources 目錄下建立一個 db.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=123
接着在 mybatis-config.xml 的 configuration 中添加 properties 節點,注意這個節點的位置必須放在首位。
<properties resource="db.properties">
</properties>
接着就可以用 properties 中的屬性去代替 xml 中的屬性
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
設定(settings)
配置檔案中的 settings 是 MyBatis 中極為重要的調整設定,它們會改變 MyBatis 的運作時行為。
常用的設定有以下幾種:

設定的配置方式如下:
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="logImpl" value="NO_LOGGING"/>
</settings>
類型别名(typeAliases)
類型别名可為 Java 類型設定一個縮寫名字。 它僅用于 XML 配置,意在降低備援的全限定類名書寫。
類型别名有兩種方式,第一種是對類取别名,第二種是指定一個包名
<mapper namespace="com.cn.mapper.UserMapper">
<select id="getUserList" resultType="com.cn.pojo.User">
select * from user;
</select>
</mapper>
第一種方式是對類取别名,這裡的 resultType 寫的是類的全限定名,我們可以在 mybatis-config.xml 中使用類型别名來簡化。typeAliases 在配置檔案中的位置需要放在 setting 後面
<typeAliases>
<typeAlias type="com.cn.pojo.User" alias="user"/>
</typeAliases>
這樣設定之後就可以在 resultType 中直接使用 user
<mapper namespace="com.cn.mapper.UserMapper">
<select id="getUserList" resultType="user">
select * from user;
</select>
</mapper>
第二種方式是指定一個包名,MyBatis 會在指定的包名路徑下搜尋需要的 JavaBean。修改配置檔案,使用包名來指定
<typeAliases>
<package name="com.cn.pojo"/>
</typeAliases>
每一個在包 com.lanqiaoyun.pojo 中的 Java Bean,在沒有注解的情況下,會使用 Bean 的首字母小寫的非限定類名來作為它的别名。 比如 com.lanqiaoyun.pojo.User 的别名為 user;若有注解,則别名為其注解值
@Alias("user")
public class User {
...
}
上方所示完整配置檔案如下:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties">
</properties>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="logImpl" value="NO_LOGGING"/>
</settings>
<typeAliases>
<package name="com.cn.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--每個mapper.xml都需要在mybatis配置檔案中進行配置-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>