SqlSessionTemplate是MyBatis-Spring的核心。这个类负责管理MyBatis的SqlSession。调用MyBatis的SQL方法。

SqlSessionTemplate是线程安全的,可以被多个DAO所共享使用。

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 {

  public 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>

MyBatis 3与spring整合之使用SqlSession的更多相关文章

  1. spring,配置文件applictionContext.xml,Mybatis mybatis.xml,springMVC spring整合springMVC mybatis

  2. Mybatis(使用)与Spring整合

    1.总结 https://pan.baidu.com/s/1kWpz7ZD  密码:tsvr 2.代码 https://pan.baidu.com/s/1mjgAeak   密码:h9j8 3.资料 ...

  3. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

  4. spring整合jpa

    有点类似SSH整合,O(∩_∩)O哈哈~ <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  5. 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题

    https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...

  6. 关于Mybatis与Spring整合之后SqlSession与mapper对象之间数量的问题。

    1,sqlsession的真实类型和数量 由于使用spring管理bean,当我们在代码中需要使用这个bean的时候,会首先去容器中找,第一次需要调用MapperFactoryBean的getObje ...

  7. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  8. Intellij IDEA +MAVEN+Jetty实现Spring整合Mybatis

    1 pom.xml(这里出现transaction错误,是版本的问题) <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  9. Spring整合MyBatis

    前言:MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的XML或注解用 ...

随机推荐

  1. BZOJ 4052: [Cerc2013]Magical GCD

    以一个数字开头的子序列的gcd种类不会超过logn种,因此去找相同gcd最长的位置,更新一下答案,复杂度O(nlogn^2) #include<cstdio> #include<al ...

  2. 关于内存 GetMemory( ) 笔试分析

    1. #include<stdio.h>#include<string.h>void GetMemory(char *p){ p=(char *)malloc(100); }i ...

  3. ligerui_ligerTree_002_利用JavaScript代码配置ligerTree节点

    利用JavaScript代码配置ligerTree节点: 源码地址:http://download.csdn.net/detail/poiuy1991719/8571255 效果图: <%@ p ...

  4. 利用API 建立Dependent Value Set

    . 建立SET fnd_flex_val_api.create_valueset_independent(v_set_name ,v_description ,v_security ,v_enable ...

  5. async fifo

    异步fifo,解决跨时钟域的数据传输问题. 由binary,gray两种counter组成,在读写domain之间,只传输gray code. 主要的设计难点在empty和full的产生中. empt ...

  6. 编译php时出现xsl错误的解决方法

    是因为系统没安装一个叫 libxslt-devel 的包, 安装上就好了. 附编译php时的常见错误: http://www.myhack58.com/Article/sort099/sort0102 ...

  7. oracle中dual表的使用

    dual表是一个虚拟表,用来和select语句一起使用.1.查看当前用户select  user from dual2.用来调用系统函数select to_char(sysdate,'yyyy-mm- ...

  8. 【python cookbook】【数据结构与算法】17.从字典中提取子集

    问题:想创建一个字典,其本身是另一个字典的子集 解决方案:利用字典推导式(dictionary comprehension)可轻松解决 # example of extracting a subset ...

  9. Android剪切板传递数据传递序列化对象数据

    一.剪切板的使用介绍 1. 剪切板对象的创建 使用剪切板会用到,ClipboardManager对象,这个对像的创建不可以使用构造方法,主要是由于没有提供public的构造函数(单例模式),需要使用A ...

  10. Lucas定理模板

    用于大组合数对p取模的计算. #include <cstdio> #include <iostream> #include <cmath> #include < ...