SqlSession(SqlSessionTemplate类) 实现Mybatis
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作为构造方法的参数来创建。
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory"/>
- </bean>
这个bean现在可以直接注入到DAO bean中。你需要在bean中添加一个SqlSession属性,就像下面的代码:
- public class UserDaoImpl implements UserDao{
- private SqlSession sqlSession;
- public void setSqlSession(SqlSession sqlSession){
- this.sqlSession = sqlSession;
- }
- public User getuser(String userId){
- return (User)sqlSession.selectOne
- ("org.mybatis.spring.sample.mapper.UserMapper.getUser",userId);
- }
- }
如下注入SqlSessionTemplate:
- <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
- <property name="sqlSession" ref="sqlSession"/>
- </bean>
SqlSession(SqlSessionTemplate类) 实现Mybatis的更多相关文章
- 工具类:mybatis中使用Threadlocal开启session及关闭session
1.线程容器,给线程绑定一个Object 内容,后只要线程不变,可以随时取出. 1.1 改变线程,无法取出内容. final ThreadLocal threadLocal = new ThreadL ...
- 创建sqlsession工具类
//1.sqlsession的获取: //类:GetSqlSession, 返回sqlsession对象,无参 public class GetSqlSession { public static S ...
- 在Idea中连接数据库并生成实体类(mybatis逆向生成实体类)
1.连接数据库 (1)按下图 , 点击view-----选择tool windows----------选择database并点击 (2)弹出Database窗口 点击加号------------选 ...
- 核心类生成-Mybatis Generator的使用
总结一下Generator的使用,首先要设计好数据表,然后修改generator.xml中的配制,接着直接运行命令就可以了. 第一步:数据库设计: 生成数据表代码: /* Navicat MySQL ...
- 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, ...
- MyBatis在Spring环境下的事务管理
MyBatis的设计思想很简单,可以看做是对JDBC的一次封装,并提供强大的动态SQL映射功能.但是由于它本身也有一些缓存.事务管理等功能,所以实际使用中还是会碰到一些问题--另外,最近接触了JFin ...
- MyBatis的Dao层注入SqlSession
有点坑爹,以前没用过Mybatis,最近才用,而且一直用Mybatis推荐的接口映射的方式,但是今天有人告诉我接口方式用得少,大多还是采用从配置文件里面读sql的方式,当然接口也是类似的,都是利用ma ...
- Mybatis SqlSessionTemplate 源码解析
As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory and at le ...
- Mybatis源码分析-SqlSessionTemplate
承接Mybatis源码解析-MapperRegistry注册mapper接口,本文将在前文基础上讲解持久层的生成 SqlSessionFactory生成 在spring中,SqlSessionFact ...
随机推荐
- python中的__init__
__init__ __init__中__表示系统默认命名,init是初始化的意思.由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去.以学生类为例,通过定义 ...
- SpringBoot 快速构建微服务体系 知识点总结
可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽 ...
- 快捷键中文版使用说明之Eclipse快捷键大全
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt ...
- codeforces 584E Anton and Ira [想法题]
题意简述: 给定一个$1$到$n(n<=2000)$的初始排列以及最终排列 我们每次可以选取位置为$i$和$j$的 并交换它们的位置 花费为$ |i-j| $ 求从初始状态变换到末状态所需最小花 ...
- JS:收集的一些Array及String原型对象的扩展实现代码
扩展Array的原型对象的方法 // 删除数组中数据 Array.prototype.del = function(n) { if (n<0) return this; return this ...
- jQuery设置checkbox 为选中状态
1设置第一个checkbox 为选中值$('input:checkbox:first').attr("checked",'checked');或者$('input:checkbox ...
- HTTP socket网络通信
import socket class WebServer(): def __init__(self): ''' 1.创建总的socket 2.监听 :param self: :param addr: ...
- Ubuntu安装护眼程序
目录 1.安装 2.配置 参考资料 一开始想在Ubuntu下安装在Windows下使用的f.lux,但是折腾了很久f.lux也没能正常运作.于是打开另一台Ubuntu电脑,将上面使用的Redshift ...
- 解决text-align: justify;浏览器、安卓手机不兼容问题
1.兼容PC端浏览器 .h_text{ text-align:justify; text-justify:inter-ideograph;width:200px;} .span_hid{ displa ...
- 超強的Linux指令解釋網站《explainshell.com》,學Linux必備!
ExplainShell 官方網站:http://explainshell.com/ 原始碼下載:https://github.com/idank/explainshell 用瀏覽器打該explain ...