yBatis3与spring整合之使用SqlSession(SqlSessionDaoTemplate类)

----------

注:这是手工编写实现的方式(其实可以直接使用注入映射器的)

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作为构造方法的参数来创建。

  1. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  2. <constructor-arg index="0" ref="sqlSessionFactory"/>
  3. </bean>

这个bean现在可以直接注入到DAO bean中。你需要在bean中添加一个SqlSession属性,就像下面的代码:

  1. public class UserDaoImpl implements UserDao{
  2. private SqlSession sqlSession;
  3. public void setSqlSession(SqlSession sqlSession){
  4. this.sqlSession = sqlSession;
  5. }
  6. public User getuser(String userId){
  7. return (User)sqlSession.selectOne
  8. ("org.mybatis.spring.sample.mapper.UserMapper.getUser",userId);
  9. }
  10. }

如下注入SqlSessionTemplate:

  1. <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
  2. <property name="sqlSession" ref="sqlSession"/>
  3. </bean>

SqlSession(SqlSessionTemplate类) 实现Mybatis的更多相关文章

  1. 工具类:mybatis中使用Threadlocal开启session及关闭session

    1.线程容器,给线程绑定一个Object 内容,后只要线程不变,可以随时取出. 1.1 改变线程,无法取出内容. final ThreadLocal threadLocal = new ThreadL ...

  2. 创建sqlsession工具类

    //1.sqlsession的获取: //类:GetSqlSession, 返回sqlsession对象,无参 public class GetSqlSession { public static S ...

  3. 在Idea中连接数据库并生成实体类(mybatis逆向生成实体类)

    1.连接数据库 (1)按下图 ,  点击view-----选择tool windows----------选择database并点击 (2)弹出Database窗口 点击加号------------选 ...

  4. 核心类生成-Mybatis Generator的使用

    总结一下Generator的使用,首先要设计好数据表,然后修改generator.xml中的配制,接着直接运行命令就可以了. 第一步:数据库设计: 生成数据表代码: /* Navicat MySQL ...

  5. Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:

    Parameter 0 of method orderSqlSessionFactory in com.config.MultipleDBConfig required a single bean, ...

  6. MyBatis在Spring环境下的事务管理

    MyBatis的设计思想很简单,可以看做是对JDBC的一次封装,并提供强大的动态SQL映射功能.但是由于它本身也有一些缓存.事务管理等功能,所以实际使用中还是会碰到一些问题--另外,最近接触了JFin ...

  7. MyBatis的Dao层注入SqlSession

    有点坑爹,以前没用过Mybatis,最近才用,而且一直用Mybatis推荐的接口映射的方式,但是今天有人告诉我接口方式用得少,大多还是采用从配置文件里面读sql的方式,当然接口也是类似的,都是利用ma ...

  8. Mybatis SqlSessionTemplate 源码解析

    As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory and at le ...

  9. Mybatis源码分析-SqlSessionTemplate

    承接Mybatis源码解析-MapperRegistry注册mapper接口,本文将在前文基础上讲解持久层的生成 SqlSessionFactory生成 在spring中,SqlSessionFact ...

随机推荐

  1. "New page after" by code

    Hi. There is a method for starting of the new page in the EngineV2: Engine.NewPage(); You can call i ...

  2. vue 项目配置sass

    1.运行npm install node-sass --save-dev npm install sass-loader --save-dev 2.打开build文件夹下面的webpack.base. ...

  3. Flask Session ,pymysql ,wtforms组件 虚拟virtualenv venv

    https://www.cnblogs.com/wupeiqi/articles/5713330.html session def create_app(): print() app=Flask(__ ...

  4. FFT&NTT数学解释

    FFT和NTT真是噩梦呢 既然被FFT和NTT坑够了,坑一下其他的人也未尝不可呢 前置知识 多项式基础知识 矩阵基础知识(之后会一直用矩阵表达) FFT:复数基础知识 NTT:模运算基础知识 单位根介 ...

  5. Selenium:多窗口切换(获取窗口句柄handle)

    我们在操作网页的时候,点击有些页面的链接,会重新打开一个窗口,我们要在新页面上操作,就得切换窗口 比如在百度首页的登录框点击注册,会重新打开一个注册的新页面,要在新页面注册,就得先切进新页面 那我们怎 ...

  6. jsbridge 原理

    https://juejin.im/post/5abca877f265da238155b6bc

  7. 继承Process类,run函数的简单使用

    #定义一个类 继承Process类 from multiprocessing import Process import os import time class Download(Process): ...

  8. VB - 修改注册表

    Vbs中修改注册表的语句主要有: 1.读注册表的关键词和值: 可以通过把关键词的完整路径传递给wshshell对象的regread方法.例如: set ws=wscript.createobject( ...

  9. JPA、Hibernate、Spring Data JPA 的关系,你懂吗?

    来源:https://my.oschina.net/u/3080373/blog/1828589 什么是JPA? 全称Java Persistence API,可以通过注解或者XML描述[对象-关系表 ...

  10. python内存管理及垃圾回收

    一.python的内存管理 python内部将所有类型分成两种,一种由单个元素组成,一种由多个元素组成.利用不同结构体进行区分 /* Nothing is actually declared to b ...