sqlsession是什么?
從
http://blog.csdn.net/hupanfeng/article/details/9238127
知道
sqlsession創建
可以看出,創建sqlsession經過了以下幾個主要步驟:
1) 從配置中獲取Environment;
2) 從Environment中取得DataSource;
3) 從Environment中取得TransactionFactory;
4) 從DataSource里獲取數據庫連接對象Connection;
5) 在取得的數據庫連接上創建事務對象Transaction;
6) 創建Executor對象(該對象非常重要,事實上sqlsession的所有操作都是通過它完成的);
7) 創建sqlsession對象。
從官網知道
In MyBatis you use the SqlSessionFactory to create an SqlSession. Once you have a session, you use it to execute your mapped statements, commit or rollback connections and finally, when it is no longer needed, you close the session. With MyBatis-Spring you don't need to use SqlSessionFactory directly because your beans can be injected with a thread safe SqlSession that automatically commits, rollbacks and closes the session based on Spring's transaction configuration.
就是使用sqlsession可以使用事務功能,在一次sqlsession中增刪改查可以回滾和提交
在spring中是怎么創建mapper實例的?
在spring中
官網:
http://www.mybatis.org/spring/zh/mappers.html
說到可以利用
<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>
利用MapperFactoryBean來進行mapper創建
讀源碼知道創建一個MapperFactoryBean就要創建一個sqlsession,,所以數據庫事務交給spring來管理
sqlsession在close時沒有close數據庫連接,只是把數據庫連接返回給數據庫連接池
一次sqlsession中update需要commite;