1. 程式人生 > >深入淺出Mybatis-與Spring整合

深入淺出Mybatis-與Spring整合

單獨使用mybatis是有很多限制的(比如無法實現跨越多個session的事務),而且很多業務系統本來就是使用spring來管理的事務,因此mybatis最好與spring整合起來使用。

前置要求

版本要求

專案

版本

下載地址

說明

mybatis

3.0及以上

https://github.com/mybatis/mybatis-3/releases

spring

3.0及以上

http://projects.spring.io/spring-framework/

mybatis-spring

1.0及以上

https://github.com/mybatis/spring/releases

spring事務配置

<!-- 自動掃描業務包 -->
<context:component-scan base-package="com.xxx.service" />

<!-- 資料來源 -->
<jee:jndi-lookup id="jndiDataSource" jndi-name="java:comp/env/jdbc/datasource" />

<!-- 配置事務 -->
<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="jndiDataSource" />
</bean>
<!-- 配置基於註解的事物aop -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

單個整合

<!-- 整合mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="jndiDataSource" />
	<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" />
	<!-- 自動配置別名 -->
	<property name="typeAliasesPackage" value="com.xxx.dto" />
</bean>

<!--建立dao bean(只需提供介面不需提供實現類 )-->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
	<property name="mapperInterface" value="com.xxx.dao.UserDao" />
	<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

我們不但要明白如何使用,更要明白為什麼要這麼使用。

SqlSessionFactoryBean是一個工廠bean,它的作用就是解析配置(資料來源、別名等)。

MapperFactoryBean是一個工廠bean,在spring容器裡,工廠bean是有特殊用途的,當spring將工廠bean注入到其他bean裡時,它不是注入工廠bean本身而是呼叫bean的getObject方法。我們接下來就看看這個getObjec方法幹了些什麼:

  public T getObject() throws Exception {
    return getSqlSession().getMapper(this.mapperInterface);
  }

看到這裡大家應該就很明白了,這個方法和我們之前單獨使用Mybatis的方式是一樣的,都是先獲取一個Sqlsession物件,然後再從Sqlsession裡獲取Mapper物件(再次強調Mapper是一個代理物件,它代理的是mapperInterface介面,而這個介面是使用者提供的dao介面)。自然,最終注入到業務層就是這個Mapper物件。

實際的專案一般來說不止一個Dao,如果你有多個Dao那就按照上面的配置依次配置即可。

如何使用批量更新

前一節講了如何注入一個mapper物件到業務層, mapper的行為依賴於配置,mybatis預設使用單個更新(即ExecutorType預設為SIMPLE而不是BATCH),當然我們可以通過修改mybatis配置檔案來修改預設行為,但如果我們只想讓某個或某幾個mapper使用批量更新就不得行了。這個時候我們就需要使用模板技術:

 <!--通過模板定製mybatis的行為 -->
<bean id="sqlSessionTemplateSimple" class="org.mybatis.spring.SqlSessionTemplate">   
	<constructor-arg index="0" ref="sqlSessionFactory" />
	<!--更新採用單個模式 -->
	<constructor-arg index="1" value="SIMPLE"/>
 </bean>
     
 <!--通過模板定製mybatis的行為 -->
<bean id="sqlSessionTemplateBatch" class="org.mybatis.spring.SqlSessionTemplate">   
	<constructor-arg index="0" ref="sqlSessionFactory" />
	<!--更新採用批量模式 -->
	<constructor-arg index="1" value="BATCH"/>
 </bean>

這裡筆者定義了兩個模板物件,一個使用單個更新,一個使用批量更新。有了模板之後我們就可以改變mapper的行為方式了:

	<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.xxx.dao.UserDao" />
		<property name="sqlSessionTemplate" ref=" sqlSessionTemplateBatch " />
	</bean>

跟上一節的mapper配置不同的是,這裡不需要配置sqlSessionFactory屬性,只需要配置sqlSessionTemplatesqlSessionFactory屬性在模板裡已經配置好了)。

通過自動掃描簡化mapper的配置

前面的章節可以看到,我們的dao需要一個一個的配置在配置檔案中,如果有很多個dao的話配置檔案就會非常大,這樣管理起來就會比較痛苦。幸好mybatis團隊也意識到了這點,他們利用spring提供的自動掃描功能封裝了一個自動掃描dao的工具類,這樣我們就可以使用這個功能簡化配置:

<!-- 採用自動掃描方式建立mapper bean(單個更新模式) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.xxx.dao" />
	<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateSimple" />
	<property name="markerInterface" value="com.xxx.dao.SimpleDao" />
</bean>
     
<!-- 採用自動掃描方式建立mapper bean(批量更新模式) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.xxx.dao" />
	<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />
	<property name="markerInterface" value="com.xxx.dao.BatchDao" />
</bean>

MapperScannerConfigurer本身涉及的spring的技術我就不多講了,感興趣且對spring原理比較瞭解的可以去看下它的原始碼。我們重點看一下它的三個屬性:

basePackage:掃描器開始掃描的基礎包名,支援巢狀掃描;

sqlSessionTemplateBeanName:前文提到的模板bean的名稱;

markerInterface:基於介面的過濾器,實現了該介面的dao才會被掃描器掃描,與basePackage是與的作用。

除了使用介面過濾外,還可使用註解過濾:

<!-- 採用自動掃描方式建立mapper bean(批量更新模式) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.xxx.dao" />
	<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />
	<property name="annotationClass" value="com.xxx.dao.BatchAnnotation" />
</bean>

annotationClass配置了該註解的dao才會被掃描器掃描,與basePackage是與的作用。

需要注意的是,兩個過濾條件只能配一個。