第五章 使用 SqlSession
原文鏈接; http://www.mybatis.org/spring/zh/sqlsession.html
在 MyBatis 中,你可以使用 SqlSessionFactory 來創建 SqlSession。一旦你獲得一個 session 之后,你可以使用它來執行映射語句,提交或回滾連接,最后,當不再需要它的時 候, 你可以關閉 session。 使用 MyBatis-Spring 之后, 你不再需要直接使用 SqlSessionFactory 了,因為你的 bean 可以通過一個線程安全的 SqlSession 來注入,基於 Spring 的事務配置 來自動提交,回滾,關閉 session。
注意通常不必直接使用 SqlSession。 在大多數情況下 MapperFactoryBean, 將會在 bean 中注入所需要的映射器。下一章節中的 MapperFactoryBean(6.1 節)會解釋這個細節。
SqlSessionTemplate
SqlSessionTemplate 是 MyBatis-Spring 的核心。 這個類負責管理 MyBatis 的 SqlSession, 調用 MyBatis 的 SQL 方法, 翻譯異常。 SqlSessionTemplate 是線程安全的, 可以被多個 DAO 所共享使用。
當調用 SQL 方法時, 包含從映射器 getMapper()方法返回的方法, SqlSessionTemplate 將會保證使用的 SqlSession 是和當前 Spring 的事務相關的。此外,它管理 session 的生命 周期,包含必要的關閉,提交或回滾操作。
SqlSessionTemplate 實現了 SqlSession 接口,這就是說,在代碼中無需對 MyBatis 的 SqlSession 進行替換。 SqlSessionTemplate 通常是被用來替代默認的 MyBatis 實現的 DefaultSqlSession , 因為模板可以參與到 Spring 的事務中並且被多個注入的映射器類所使 用時也是線程安全的。相同應用程序中兩個類之間的轉換可能會引起數據一致性的問題。
SqlSessionTemplate 對象可以使用 SqlSessionFactory 作為構造方法的參數來創建。
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
這個 bean 現在可以直接注入到 DAO bean 中。你需要在 bean 中添加一個 SqlSession 屬性,就像下面的代碼:
public class UserDaoImpl implements UserDao { private SqlSession sqlSession; public void setSqlSession(SqlSession sqlSession) { this.sqlSession = sqlSession; } public User getUser(String userId) { return (User) sqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); } }
如下注入 SqlSessionTemplate:
<bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl"> <property name="sqlSession" ref="sqlSession" /> </bean>
SqlSessionTemplate 有一個使用 ExecutorType 作為參數的構造方法。這允許你用來 創建對象,比如,一個批量 SqlSession,但是使用了下列 Spring 配置的 XML 文件:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> <constructor-arg index="1" value="BATCH" /> </bean>
現在你所有的語句可以批量操作了,下面的語句就可以在 DAO 中使用了。
public void insertUsers(User[] users) { for (User user : users) { sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", user); } }
注意,如果所需的執行方法和默認的 SqlSessionFactory 設置不同,這種配置風格才 能使用。
對這種形式需要說明的是當這個方法被調用時,不能有一個存在使用不同 ExecutorType 運行的事務。也要保證在不同的事務中,使用不同執行器來調用 SqlSessionTemplate 時, (比如 PROPAGATION_REQUIRES_NEW)或完全在一個事務外面。
SqlSessionDaoSupport
SqlSessionDaoSupport 是 一 個 抽象 的支 持 類, 用來 為你 提供 SqlSession 。 調 用 getSqlSession()方法你會得到一個 SqlSessionTemplate,之后可以用於執行 SQL 方法, 就像下面這樣:
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { public User getUser(String userId) { return (User) getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); } }
通常 MapperFactoryBean 是這個類的首選,因為它不需要額外的代碼。但是,如果你 需要在 DAO 中做其它非 MyBatis 的工作或需要具體的類,那么這個類就很有用了。
SqlSessionDaoSupport 需要一個 sqlSessionFactory 或 sqlSessionTemplate 屬性來 設 置 。 這 些 被 明 確 地 設 置 或 由 Spring 來 自 動 裝 配 。 如 果 兩 者 都 被 設 置 了 , 那 么 SqlSessionFactory 是被忽略的。
假設類 UserMapperImpl 是 SqlSessionDaoSupport 的子類,它可以在 Spring 中進行如 下的配置:
<bean id="userMapper" class="org.mybatis.spring.sample.mapper.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>