mybatis spring sqlsession

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;

posted @ 2017-12-07 19:41  每天一点积累  阅读(3834)  评论(1编辑  收藏  举报