关于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="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
这个bean现在可以直接注入到DAO bean中。你需要在bean中添加一个SqlSession属性,就像下面的代码:
public class UserDaoImpl implements UserDao{
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
this.sqlSessionTemplate = sqlSessionTemplate;
}
public User getUser(String userId){
return (User)sqlSessionTemplate.selectOne ("org.mybatis.spring.sample.mapper.UserMapper.getUser",userId);
}
}
如下注入SqlSessionTemplate:
<bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
关于sqlSessionTemplate的更多相关文章
- 使用SqlSessionTemplate实现数据库的操作
EmployeeMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE map ...
- 没有必要去指定SqlSessionFactory或SqlSessionTemplate
<!-- 自动注册mybatis mapper bean --><!-- 注意,没有必要去指定SqlSessionFactory或SqlSessionTemplate, 因为 ...
- Mybatis SqlSessionTemplate 源码解析
As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory and at le ...
- Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
之前一直用mybatis+mybatis-spring-1.1.1,系统升级mybatis使用后 mybatis-spring-1.2.2, 再其他配置均为改动的情况下执行出错: Property ' ...
- Mybatis-Spring SqlSessionTemplate 源码解析
在使用Mybatis与Spring集成的时候我们用到了SqlSessionTemplate 这个类. <bean id="sqlSession" class="or ...
- Mybatis源码分析-SqlSessionTemplate
承接Mybatis源码解析-MapperRegistry注册mapper接口,本文将在前文基础上讲解持久层的生成 SqlSessionFactory生成 在spring中,SqlSessionFact ...
- Mybatis(基于SqlSessionTemplate的实现) + Spring 练习实战
mybatis学习篇:上次使用映射接口实现Mybatis,有不方便指出就是需要接口,且需要保证接口上不能存在其他的代理.这次通过SqlSessionTemplate基于模板类实现Mybatis,总的来 ...
- 设计模式_代理模式_在SqlSessionTemplate(Spring)中的应用
1.SqlSessionTemplate的构造函数,根据传入的SqlSessionFactory和ExecutorType创建一个Spring管理的SqlSession,并生成SqlSession的动 ...
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- Spring集成MyBatis的使用-使用SqlSessionTemplate
Spring集成MyBatis的使用 Spring集成MyBatis,早期是使用SqlSessionTemplate,当时并没有用Mapper映射器,既然是早期,当然跟使用Mapper映射器是存在一些 ...
随机推荐
- IT痴汉的工作现状36-做好准备再上路
软件开发流程管理是採用瀑布式好还是敏捷好? 如今非常多人会选择敏捷.由于眼下的现状是需求的变化是一天一个样,这是当前(移动)互联网的飞速发展所带来的.当我们仍採用原始的先做全盘的计划.然后在按部就班的 ...
- 用Fiddler 发送post请求
在调试web api的时候,若是get 请求,可以直接在浏览器里查看结果,如果是put,或者post请求在浏览器地址栏里就没有办法了. 下面介绍一下,如何利用fiddler模拟post请求. 也可以用 ...
- kettle--组件(2)--计算器
组件如下: 对计算类型的说明如下: The table below contains descriptions associated with the calculator step: Functio ...
- swift向方法传数组参数的语法
总是记不住向方法中传数组参数的语法,所以记录一下. func calculateStatistics(scores:[Int]) -> (min:Int,max:Int,sum:Int) { v ...
- 如何优雅的使用RabbitMQ(转载)
RabbitMQ无疑是目前最流行的消息队列之一,对各种语言环境的支持也很丰富,作为一个.NET developer有必要学习和了解这一工具.消息队列的使用场景大概有3种: 1.系统集成,分布式系统的设 ...
- 关于Go语言daemon启动的方法.
昨天搞了个文件共享的小程序,遇见了意见蛋疼的事,就是启动之后终端不能关闭,不然程序也会随着关闭. 我的解决方法: nohup ./httpserver & nohup这个命令能够把程序放后台执 ...
- 简单记录一次ORA-00600 kcratr_nab_less_than_odr
当前具体报错已经没有了,仅仅有对应图.參考EYGLE一篇文章中数据: 1.故障现象 数据库版本号11G,错误类似下面: ORA-00600: 内部错误代码, 參数: [kcratr_nab_less_ ...
- 点滴积累【JS】---JS小功能(setInterval实现图片效果显示时间)
效果: 代码: <head runat="server"> <title></title> <script type="text ...
- SQL server账号无法登陆
- 编译JDK1.7
1. 背景 想要一探JDK内部的实现机制,最便捷的路径之一就是自己编译一套JDK. 2. 获取JDK源码 从http://openjdk.java.net/下载源码,如 openjdk-6-src-b ...