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. codeforces 111A/112C Petya and Inequiations

    题目:Petya and Inequiations传送门: http://codeforces.com/problemset/problem/111/A http://codeforces.com/p ...

  2. sublime常用基础插件合集

    插件介绍 Package Control 功能:安装包管理简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能使用方法:快捷键 Ctrl+Shift+P,输入 install 选中In ...

  3. python中chr()函数和ord()函数的用法

    一,chr()函数 格式:Chr(<数值表达式>)   说明:函数返回值类型为String,其数值表达式值取值范围为0~255.   例如:Print Chr(78),结果显示:N.   ...

  4. CSS-给Font Awesome拓展Base64编码的图标

    和 fa 一样设置到::before中就行了,不过 fa 是直接设置内容,这里用的背景图 .fa-science-garden::before { content: ""; dis ...

  5. 如果遇到找不到元素如何处理? Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"investmentframe"}

    常见几种原因与应对,详细参见http://www.blogjava.net/qileilove/archive/2014/12/11/421309.html 1,动态ID无法找到,用xpath路径解决 ...

  6. jenkins构建后操作archive the artfacts的用法

    参考: https://blog.csdn.net/liqiangeastsun/article/details/79062806 Jenkins构建完成存档 Archive the artifact ...

  7. MVC 入门

    MVC是什么? MVC是一个框架模式,它用于把应用程序的输入.处理和输出进行强制性的分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.最典型的MVC就是JSP+S ...

  8. js中slice、splice、substr、split方法

    1.slice 可用于数组与字符串,返回一个新的数组,原数组不改变,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素. 在string中 slice(start,e ...

  9. Asp.Net Core 第05局:读取配置

    前言 本文介绍Asp.Net Core 读取配置文件. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 前期准备             1.添加app.j ...

  10. Java + selenium 元素定位(2)之By LinkText/PartialLinkText

    本章介绍的两种方法都是对于网页上的文字链接的定位操作.根据名字,我们就可以看出来,这两者其实很相似,那么他们的不同在哪里呢. By LinkText()方法,是对一个的网页超链接,我们所需要输入的关键 ...