MyBatis 3 發布時 Spring 3 已定型,故 Spring 原生不支援 MyBatis 3。MyBatis 社區開發了 MyBatis-Spring 整合包填補空白。本文詳解整合核心組件配置與最佳實踐。
MyBatis-Spring 以 SqlSessionFactoryBean 取代原生 SqlSessionFactoryBuilder,封裝了 SqlSession 獲取流程。在 Spring applicationContext.xml 中定義:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml" />
<property name="typeAliasesPackage" value="com.tiantian.mybatis.model" />
</bean>
關鍵屬性詳解:
dataSource (必填):資料源,通常為 DBCP/ Druid 連接池。mapperLocations:Mapper XML 文件路徑。若 XML 與接口同包可省略。configLocation:指定 MyBatis 全局配置文件,後續屬性會覆蓋文件內同名配置。typeAliasesPackage:實體類包路徑,自動以簡類名為別名(如 com.xxx.User → User)。typeAliases:陣列方式指定別名類,優先級高於包掃描。plugins:攔截器陣列(如分頁插件)。typeHandlersPackage / typeHandlers:類型處理器配置。通過 MapperFactoryBean 將 Mapper 接口註冊為 Spring Bean:
<bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.tiantian.mybatis.mapper.BlogMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
隨後即可在 Service 中 @Autowired 注入 BlogMapper 使用。
手動聲明每個 Mapper 繁瑣,可使用掃描器自動註冊:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tiantian.mybatis.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
掃描指定包下所有接口自動生成代理 Bean,接口名首字母小寫為 Bean 名 (如 UserMapper → userMapper)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.tiantian.mybatis" />
<context:property-placeholder location="classpath:config/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml" />
<property name="typeAliasesPackage" value="com.tiantian.mybatis.model" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tiantian.mybatis.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
namespace 必須對應 Mapper 接口全限定名。parameterType / resultType / resultMap 靈活映射。useGeneratedKeys="true" keyProperty="id" (適用 MySQL/SQL Server)。掌握以上配置,即可實現 MyBatis 與 Spring 無縫整合,享受 Spring 事務管理、依賴注入等企業級特性。