java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事務
一、版本限制
參看地址:http://www.mybatis.org/spring/
二、使用入門
2.1、pom
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>x.x.x</version> </dependency>
2.2、SqlSessionFactoryBean創建
在 Spring 的 XML 配置文件中:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean>
要註意 SqlSessionFactory 需要一個 DataSource(數據源) 。這可以是任意 的 DataSource,配置它就和配置其它 Spring 數據庫連接一樣。
2.3、數據映射器
假設你定義了一個如下的數據 mapper 接口:
public interfaceUserMapper { @Select("SELECT * FROM users WHERE id = #{userId}") User getUser(@Param("userId") String userId); }
那麽可以使用 MapperFactoryBean,像下面這樣來把接口加入到 Spring 中:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
註意,所指定的映射器類必須是一個接口,而不是具體的實現類。在這個示例中,註解被用來指定 SQL 語句,但是 MyBatis 的映射器 XML 文件也可以用。
一旦配置好,你可以用註入其它任意 Spring 的 bean 相同的方式直接註入映射器到你的 business/service 對象中。MapperFactoryBean 處理 SqlSession 的創建和關閉它。如果使用 了 Spring 的事務,那麽當事務完成時,session 將會提交或回滾。最終,任何異常都會被翻 譯成 Spring 的 DataAccessException 異常。
調用 MyBatis 數據方法現在只需一行代碼:
public class FooServiceImpl implements FooService { private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public User doSomeBusinessStuff(String userId) { return this.userMapper.getUser(userId); } }
三、SqlSessionFactoryBean
在基本的 MyBatis 中,session 工廠可以使用 SqlSessionFactoryBuilder 來創建。而在 MyBatis-Spring 中,則使用 SqlSessionFactoryBean 來替代。
3..1、創建
要創建工廠 bean,放置下面的代碼在 Spring 的 XML 配置文件中:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean>
要註意 SqlSessionFactoryBean 實現了 Spring 的 FactoryBean 接口(請參考 Spring 文 檔的 3.8 章節)這就說明了由 Spring 最終創建的 bean 不是 SqlSessionFactoryBean 本身, 。 而是工廠類的 getObject()返回的方法的結果。這種情況下,Spring 將會在應用啟動時為你 創建 SqlSessionFactory 對象,然後將它以 SqlSessionFactory 為名來存儲。在 Java 中, 相同的代碼是:
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); SqlSessionFactory sessionFactory = factoryBean.getObject();
在一般的 MyBatis-Spring 用法中, 你不需要直接使用 SqlSessionFactoryBean 或和其對 應的 SqlSessionFactory。相反,session 工廠將會被註入到 MapperFactoryBean 或其它擴 展了 SqlSessionDaoSupport 的 DAO(Data Access Object,數據訪問對象)中
3.2、屬性
SqlSessionFactory 有一個單獨的必須屬性,就是 JDBC 的 DataSource。這可以是任意 的 DataSource,其配置應該和其它 Spring 數據庫連接是一樣的。
一個通用的屬性是 configLocation,它是用來指定 MyBatis 的 XML 配置文件路徑的。 如果基本的 MyBatis 配置需要改變, 那麽這就是一個需要它的地方。 通常這會是<settings> 或<typeAliases>的部分。
要註意這個配置文件不需要是一個完整的 MyBatis 配置。確切地說,任意環境,數據源 和 MyBatis 的事務管理器都會被忽略。SqlSessionFactoryBean 會創建它自己的,使用這些 值定制 MyBatis 的 Environment 時是需要的。
如果 MyBatis 映射器 XML 文件在和映射器類相同的路徑下不存在,那麽另外一個需要 配置文件的原因就是它了。使用這個配置,有兩種選擇。第一是手動在 MyBatis 的 XML 配 置文件中使用<mappers>部分來指定類路徑。第二是使用工廠 bean 的 mapperLocations 屬 性。
mapperLocations 屬性使用一個資源位置的 list。 這個屬性可以用來指定 MyBatis 的 XML 映射器文件的位置。 它的值可以包含 Ant 樣式來加載一個目錄中所有文件, 或者從基路徑下 遞歸搜索所有路徑。比如:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" /> </bean>
這會從類路徑下加載在 sample.config.mappers 包和它的子包中所有的 MyBatis 映射器 XML 文件。
在容器環境管理事務中,一個可能需要的屬性是 transactionFactoryClass。請參考 第四章(4.2 節)中來查看有關部分。
1.3以後增加了configuration property
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configuration"> <bean class="org.apache.ibatis.session.Configuration"> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> </property> </bean>
四、事務
一個使用 MyBatis-Spring 的主要原因是它允許 MyBatis 參與到 Spring 的事務管理中。而 不是給 MyBatis 創建一個新的特定的事務管理器,MyBatis-Spring 利用了存在於 Spring 中的 DataSourceTransactionManager。
一旦 Spring 的 PlatformTransactionManager 配置好了,你可以在 Spring 中以你通常的做 法來配置事務。@Transactional 註解和 AOP(Aspect-Oriented Program,面向切面編程,譯 者註)樣式的配置都是支持的。在事務處理期間,一個單獨的 SqlSession 對象將會被創建 和使用。當事務完成時,這個 session 會以合適的方式提交或回滾。
一旦事務創建之後,MyBatis-Spring 將會透明的管理事務。在你的 DAO 類中就不需要額 外的代碼了。
4.1、標準事務
要 開 啟 Spring 的 事 務 處 理 , 在 Spring 的 XML 配 置 文 件 中 簡 單 創 建 一 個 DataSourceTransactionManager 對象:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
指定的 DataSource 一般可以是你使用 Spring 的任意 JDBC DataSource。這包含了連接 池和通過 JNDI 查找獲得的 DataSource。
要註意, 為事務管理器指定的 DataSource 必須和用來創建 SqlSessionFactoryBean 的 是同一個數據源,否則事務管理器就無法工作了。
4.2、容器管理事務
如果你正使用一個 JEE 容器而且想讓 Spring 參與到容器管理事務(Container managed transactions,CMT,譯者註)中,那麽 Spring 應該使用 JtaTransactionManager 或它的容 器指定的子類來配置。做這件事情的最方便的方式是用 Spring 的事務命名空間:
<tx:jta-transaction-manager />
在這種配置中,MyBatis 將會和其它由 CMT 配置的 Spring 事務資源一樣。Spring 會自動 使用任意存在的容器事務,在上面附加一個 SqlSession。如果沒有開始事務,或者需要基 於事務配置,Spring 會開啟一個新的容器管理事務。
註 意 , 如 果 你 想 使 用 CMT , 而 不 想 使 用 Spring 的 事 務 管 理 , 你 就 必 須 配 置 SqlSessionFactoryBean 來使用基本的 MyBatis 的 ManagedTransactionFactory 而不是其 它任意的 Spring 事務管理器:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="transactionFactory"> <bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" /> </property> </bean>
4.3、編程式事務
MyBatis 的 SqlSession 提供指定的方法來處理編程式的事務。 但是當使用 MyBatis-Spring 時, bean 將會使用 Spring 管理的 SqlSession 或映射器來註入。 那就是說 Spring 通常是處理 事務的。
你 不 能 在 Spring 管 理 的 SqlSession 上 調 用 SqlSession.commit() , SqlSession.rollback() 或 SqlSession.close() 方 法 。 如 果 這 樣 做 了 , 就 會 拋 出 UnsupportedOperationException 異常。註意在使用註入的映射器時不能訪問那些方法。
無論 JDBC 連接是否設置為自動提交, SqlSession 數據方法的執行或在 Spring 事務之外 任意調用映射器方法都將會自動被提交。
如果你想編程式地控制事務,請參考 Spring 手冊的 10.6 節。這段代碼展示了如何手動 使用在 10.6.2 章節描述的 PlatformTransactionManager 來處理事務。
DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = txManager.getTransaction(def); try { userMapper.insertUser(user); } catch (MyException ex) { txManager.rollback(status); throw ex; } txManager.commit(status);
註意這段代碼展示了一個映射器,但它也能和 SqlSession 一起使用。
java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事務