原生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的更多相关文章

  1. MyBatis(3.2.3) - Integration with Spring

    MyBatis-Spring is a submodule of the MyBatis framework, which provides seamless integration with the ...

  2. SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis

    一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...

  3. MyBatis之会话Session原理

    MyBatis 之会话 Session 执行逻辑 1.SQL 会话工厂构建器类 SqlSessionFactoryBuilder 的 build 方法用于构建 SqlSessionFactory 类的 ...

  4. SSH中将hibernate托管给spring获取session的方法

    import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionF ...

  5. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  6. MyBatis 缓存问题 session

    iBatis(MyBatis)开启缓存后,通过外部程序修改或者删除数据库记录,如何让Cache清除?5 当其外部的数据库连接甚至是数据库管理系统,对数据库进行了更改,iBatis(MyBatis)的缓 ...

  7. springmvc+mybatis+redis的session共享+maven管理

    负载均衡搭建:http://www.cnblogs.com/guoyansi19900907/p/8717746.html redis安装:http://www.cnblogs.com/guoyans ...

  8. mybatis第二天_拓展——与spring整合以及逆向工程

    一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...

  9. mybatis 热部署xml文件(spring boot和springmvc两种方式)

    参考:http://thinkgem.iteye.com/blog/2304557 步骤:1.创建两个java类 (1)MapperRefresh.java   :用于刷新mapper (2)SqlS ...

随机推荐

  1. Android相关资源

    各类黑客大会资料 https://infocon.org/cons/ 各类课程.视频 https://github.com/Developer-Y/cs-video-courses#security ...

  2. linux find 用法和常见用例

    Linux中find常见用法示例 ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \; 本人qq群也有许多 ...

  3. Django【第4篇】:Django之模板继承

    jango框架之模板继承和静态文件配置 一.模板继承 目的是:减少代码的冗余 语法: {% block classinfo %} {% endblock %} 具体步骤: 1.创建一个base.htm ...

  4. 2019年8月5日~8月11日 第六周JAVA学习总结

    本周主要进行了对JAVA基础的学习和PTA代码的编写,平均每天花在学习上的时间约3个小时,花在代码上的时间约2个小时,花在解决问题上的时间约1.5个小时. 下周打算继续JAVA的学习,完成java面向 ...

  5. Python---进阶---捕获异常

    一.编写一个计算减法的方法,当第一个数小于第二个数时,抛出“被减数不能小于减数”的异常 ------------------------------------------------- def ji ...

  6. 使用idea对XML的增删改查

    XML:是一种可扩展标记性的语言,与java语言无关,它可以自定义标签. 1.首先需要到导入Dom4j架包,与自己所时候的ide关联 2.编写自己的xml文件,入上图所示(里面的所有元素及元素中的属性 ...

  7. overload(重载) 和 override(重写)的区别

    overload(重载): 重载是基于一个类中,方法名相同,参数列表不同(如果参数列表相同时,参数的类型要不同),与返回值和访问修饰符都无关 如果在面试中就直接说:"同名不同参"  ...

  8. 使用IDEA自动生成Java实体类

    在上一篇帖子里,我们已经通过idea连接上了数据库,这里,通过IDEA自带的功能来根据数据库字段生成POJO 1. 选中一张表,右键--->Scripted Extensions--->选 ...

  9. ExoPlayer + 边缓存边播放

    在此基础上改动:https://www.cnblogs.com/candyzhmm/p/9957928.html private void openPlayer(String videoUrl) { ...

  10. PHP培训教程 php生成WAP页面

    WAP(无线通讯协议)是在数字移动电话.个人手持设备(PDA等)及计算机之间进行通讯的开放性全球标准.由于静态的WAP页面在很多方面不能满足用户个性化的服务请求,因此通过WAP服务器端语言产生动态的W ...