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. struts配置通配符*来匹配方法,实现动态调用

    01:web.xml中配置,启动struts2 <?xml version="1.0" encoding="UTF-8"?> <web-app ...

  2. react 绑定事件

    1.显示隐藏 2.输入框输入内容,立即显示出来 代码如下: 注意:版本 React v15.0.1 ReactDOM v15.0.1 browser.min.js是编译文件,将代码解析为浏览器识别的j ...

  3. test generation和MBIST

    The problem of test generation Random test generation Deterministic algorithm for test generation fo ...

  4. CCS3属性之text-overflow:ellipsis;的用法和注意之处

    语法: text-overflow:clip | ellipsis 默认值:clip 适用于:所有元素 clip: 当对象内文本溢出时不显示省略标记(...),而是将溢出的部分裁切掉. ellipsi ...

  5. windows下gvim与gcc的一键环境的搭建

    此处略去在windows中配置gcc的方法.默认你已经能够在命令提示符下直接使用gcc了. 其实就是写了一个vimscript的.vim文件,然后在 _vimrc 中使用source命令引用进来. 以 ...

  6. [Sinatra、Mongo] Mongo

    Mongo is a document-oriented database. Install the required gems: gem install mongo gem install bson ...

  7. NOIP201205Vigenère密码

                                   NOIP201205Vigenère密码 [问题描述]   16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密 ...

  8. HDU4288:Coder(线段树单点更新版 && 暴力版)

    Problem Description In mathematics and computer science, an algorithm describes a set of procedures ...

  9. linux常用命令简单介绍(netstat,awk,top,tail,head,less,more,cat,nl)

    1.netstat netstat -tnl | grep 443 (查看443端口是否被占用) root用户,用netstat -pnl | grep 443 (还可显示出占用本机443端口的进程P ...

  10. centos 6 7更改主机名 命令添加ip

    6:vi /etc/sysconfig/network 中hostname =主机名 vi /etc/hosts  添加127.0.0.1相应的主机名 7: hostnamectl set-hostn ...