mybatis如何把session托管给spring
原生mybatis创建SqlSession的流程:
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("cn.jarjar.dao.BlogMapper.insertBlog", blog);
sqlSession.commit(true)
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback(true);
} finally {
sqlSession.close();
}
缺点:
原生seesion操作就像原始的JDBC对象一样,必须按照:新建连接->执行SQL->提交(查询不需要)->如果操作数据存在异常需要回滚->释放数据库连接。
每个SqlSession新建之后必须释放,不然会造成数据库连接泄露的危险
SqlSession是个有状态的对象,是无法进行复用的,所以只能局限于request或者方法的范围,也就是所谓的线程不安全
把seesion托管给spring:
SqlSessionTemplate
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,PersistenceExceptionTranslator exceptionTranslator) {
notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
notNull(executorType, "Property 'executorType' is required");
this.sqlSessionFactory = sqlSessionFactory;
this.executorType = executorType;
this.exceptionTranslator = exceptionTranslator;
this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),new Class[] { SqlSession.class },new SqlSessionInterceptor());
}
SqlSessionInterceptor
private class SqlSessionInterceptor implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//创建sqlSession对象
SqlSession sqlSession = getSqlSession(SqlSessionTemplate.this.sqlSessionFactory,SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator);
try {
//通过sqlSession对象执行真正的crud操作
Object result = method.invoke(sqlSession, args);
return result;
} catch (Throwable t) {
//异常处理
}finally {
if (sqlSession != null) {
// 释放sqlSession
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
}
}
}
}
托管流程:spring创建了SqlSessionTemplate,在创建SqlSessionTemplate的时候提供了方法拦截器 SqlSessionInterceptor;SqlSessionTemplate实现了SqlSession接口,所以SqlSessionInterceptor会对SqlSessionTemplate中所有SqlSession接口定义的方法进行拦截;也就是说,整合spring之后的crud操作都会经过SqlSessionTemplate类,并且所有crud方法会被SqlSessionInterceptor拦截;最终SqlSessionTemplate通过代理拦截,并且通过SqlSessionHolder实现的sqlsession线程安全和自动新建和释放连接。
mybatis如何把session托管给spring的更多相关文章
- MyBatis(3.2.3) - Integration with Spring
MyBatis-Spring is a submodule of the MyBatis framework, which provides seamless integration with the ...
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...
- MyBatis之会话Session原理
MyBatis 之会话 Session 执行逻辑 1.SQL 会话工厂构建器类 SqlSessionFactoryBuilder 的 build 方法用于构建 SqlSessionFactory 类的 ...
- SSH中将hibernate托管给spring获取session的方法
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionF ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- MyBatis 缓存问题 session
iBatis(MyBatis)开启缓存后,通过外部程序修改或者删除数据库记录,如何让Cache清除?5 当其外部的数据库连接甚至是数据库管理系统,对数据库进行了更改,iBatis(MyBatis)的缓 ...
- springmvc+mybatis+redis的session共享+maven管理
负载均衡搭建:http://www.cnblogs.com/guoyansi19900907/p/8717746.html redis安装:http://www.cnblogs.com/guoyans ...
- mybatis第二天_拓展——与spring整合以及逆向工程
一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...
- mybatis 热部署xml文件(spring boot和springmvc两种方式)
参考:http://thinkgem.iteye.com/blog/2304557 步骤:1.创建两个java类 (1)MapperRefresh.java :用于刷新mapper (2)SqlS ...
随机推荐
- vue项目中打包background背景路径问题
项目中图片都放在src/img文件夹,img和background-image引用都用相对路径,即../../这种形式 在打包build的设置路径assetsPublicPath: ‘./‘,然后那些 ...
- MongoDB的分页排序
我们已经学过MongoDB的 find() 查询功能了,在关系型数据库中的选取(limit),排序(sort) MongoDB中同样有,而且使用起来更是简单 首先我们看下添加几条Document进来 ...
- 设计模式Design Pattern(1)--简介
什么是设计模式? 软件开发人员在长期实践中总结出来的解决特定问题的一套解决方案. 对象设计原则 计模式主要是基于以下的面向对象设计原则. 对接口编程而不是对实现编程. 优先使用对象组合而不是继承. 设 ...
- eclips 中的 svn 更新报错,被锁住解决办法
svn更新不下来,在文件的目录上选择 Team,然后选择 Refresh/Cleanup ,然后就可以了,这个方法我自己试有效,做为参考,对其它情况不一定有效 报错如下 解决方法
- LeetCode--128--最长连续序列(python)
给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2]输出: 4解释: 最长连续序列是 [1, 2, 3, ...
- vue路由传参并跳转页面
在vue项目中参数的传递可以使用本地缓存或者Vuex,那么vue能不能像小程序一样路由传参呢,显然是可以的而且非常简单 方式一:query传参 //传参 go(){ that.$router.push ...
- Mysql数据库表类型
MySQL的数据表类型很多,其中比较重要的是MyISAM,InnoDB这两种. 这两种类型各有优缺点,需要根据实际情况选择适合的,MySQL支持对不同的表设置不同的类型.下面做个对比: MyISA ...
- 搭建 .Net RabbitMQ 开发环境
开发环境,window 10 64位,VS2017,系统账号需要用administrator. 1 先需要安装erlang语言开发包,一路默认安装就是了,地址:http://www.erlang.or ...
- hihocoder1286 : 子矩阵求和
http://hihocoder.com/problemset/problem/1286 题解 NB分析题. 首先我们令\(s[i][j]\)表示以\((i,j)\)为左上角的矩形的权值和. 因为\( ...
- SpringBoot属性配置-第三章
1.application.yml配置#自定义参数对象book: name: A id: 1 page: 100 2.创建实体类: /** * @Auther: youqc * @Date: 2018 ...