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. 暑期训练 CF套题

    CodeForces 327A 题意:有n个数,都是0或1,然后必须执行一次操作,翻转一个区间,里面的数0变1,1变0,求最多1的数量 思路:最开始我写的最大字段和,后面好像写搓了,然后我又改成暴力, ...

  2. 【靶场练习_sqli-labs】SQLi-LABS Page-3 (Stacked Injections)

    Less-39: ?id=1 and 1 ,?id=1 and 1 : 回显不同,数字型 ?id=0 union select 1,2,group_concat(table_name) from in ...

  3. Transition 过渡/转场动画(一)

    UIViewController 的转场效果 当viewController通过push 或 present 进行转场时, 系统自带的动画是从右侧push进来一个新的viewControler (或从 ...

  4. 基于Diff机制的多个状态合并

    1. 场景 假设一个系统System在某一时刻的状态可以用State A来表示[State里面包含着一些元素的集合]: 1: State A = [element_0, element_1,……,el ...

  5. (转)SQL注入原理

    原文地址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html SQL Injection:就是通过把SQL命令插入到Web表单递交或 ...

  6. 2019 SCUT SE 新生训练第四波 L - Boxes in a Line——双向链表

    先上一波题目 https://vjudge.net/contest/338760#problem/L 这道题我们维护一个双向链表 操作1 2 3 都是双向链表的基本操作 4操作考虑到手动将链表反转时间 ...

  7. [fw]用Kprobes调试(debug)内核

    Kprobes是一种运行时动态调试内核的机制, 你可以用它设置断点并收集调试信息, 甚至改变内核行为. Kprobes分三种, 普通kprobes以及基于普通kprobes的jprobes和kretp ...

  8. MySQLSyntaxErrorException: Row size too large 转摘自:https://confluence.atlassian.com/display/CONFKB/MySQLSyntaxErrorException%3A+Row+size+too+large

    Symptoms The following appears in the atlassian-confluence.log: Caused by: com.mysql.jdbc.exceptions ...

  9. 用java 调用oracle存储过程总结

    SSM-Mybatis调用Oracle存储过程返回结果集(游标)示例 https://www.jianshu.com/p/0ae6d9d66d61 用java调用oracle存储过程总结 //1.ca ...

  10. APScheduler的简单记录

    此工具作为 定时任务调度 系统,在日常业务中经常使用,如定时获取第三方数据,定时清理数据 等等: 定时任务 和 业务逻辑 编写方式 一般有2种: 以 定时 清理db数据为例,在flask中,如下: 1 ...