Spring集成MyBatis的使用-使用SqlSessionTemplate
Spring集成MyBatis的使用
Spring集成MyBatis,早期是使用SqlSessionTemplate,当时并没有用Mapper映射器,既然是早期,当然跟使用Mapper映射器是存在一些区别的,比如映射文件命名空间不需要跟接口名一样,接口中的方法不一定跟sql的id一样,通过它的SqlSessionTemplate的使用,在具体实现类中可以实现找到对应的sql
Spring-Mybatis集成方式二-使用SqlSessionTemplate
step1 导包:spring-webmvc,mybatis,mybatis-spring,dbcp,ojdbc,spring-jdbc,junit
同Mapper映射器中的配置方式
step2 添加Spring配置文件,不再需要Mybatis的配置文件,可以在Spring配置文件中,添加SqlSessionFactoryBean来代替,且不需要配置MapperScannerConfigurer,而是配置SqlSessionTemplate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org /schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 配置连接池 -->
<!-- 读取属性文件 -->
<util:properties id="db" location="classpath:config.properties">
</util:properties>
<!-- 配置连接池,可以参考DBUtil的方法,这里采用spring创建连接池-->
<!-- destroy-method方法作用是:当spring容器关闭后,会将连接释放回到连接池,避免资源浪费 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{db.driver}"/>
<property name="url" value="#{db.url}" />
<property name="username" value="#{db.user}" />
<property name="password" value="#{db.pwd}" />
</bean> <!-- 配置SqlSessionFactoryBean -->
<bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定连接资源 -->
<property name="dataSource" ref="ds"/>
<!-- 指定映射文件:entity包下的所有后缀xml的映射文件 -->
<property name="mapperLocations" value="classpath:entity/*.xml"/>
</bean> <!-- 配置SqlSessionTemplate -->
<bean id="sst" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 采用构造器方式注入SqlSessionFactoryBean,注入第一个参数 -->
<constructor-arg index="0" ref="ssfb"></constructor-arg>
</bean> <!-- 配置组件扫描 -->
<context:component-scan base-package="dao"></context:component-scan>
</beans>
step3 写实体类,属性名和表格的字段名一样
同Mapper映射器中的配置方式
step4 EmpMapper.xml映射文件,namespace不再要求等同接口名,可以随便取,如取名newname,后续将在具体实现类中使用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="newname"><!-- 如果不是采用Mapper映射器,命名空间随便写,不做要求 -->
<!-- id要求唯一
parameterType:填写实体类的完整名字
-->
<!-- 插入语句 -->
<insert id="save" parameterType="entity.Employee">
INSERT INTO T_TABLE VALUES(6,#{name},#{age})
</insert>
<!-- 根据id查询语句 -->
<select id="findByID" parameterType="int" resultType="entity.Employee">
SELECT * FROM T_TABLE WHERE ID=#{id}
</select>
<!-- 查询所有结果,不需要参数类型 -->
<select id="findAll" resultType="entity.Employee">
SELECT * FROM T_TABLE
</select>
<!-- 修改操作 -->
<update id="modify" parameterType="entity.Employee">
UPDATE T_TABLE SET NAME=#{name},AGE=#{age} WHERE ID=#{id}
</update>
<!-- 删除操作 -->
<delete id="delete" parameterType="int">
DELETE FROM T_TABLE WHERE ID=#{ididid}
</delete> <!-- 返回map类型的结果 -->
<!-- 也可以将返回结果简写成map,map即为java.util.Map -->
<select id="findOne" parameterType="int" resultType="java.util.Map">
SELECT * FROM T_TABLE WHERE ID=#{id}
</select> <!-- 使用resultMap解决表的字段名和实体类的属性名不一致的情况 -->
<resultMap id="resultMapID" type="entity.NewEmployee">
<result property="empID" column="id" />
<result property="empName" column="name" />
<result property="empAge" column="age" />
</resultMap> <select id="findOneByNewEmp" parameterType="int" resultMap="resultMapID">
SELECT * FROM T_TABLE WHERE ID=#{id}
</select> </mapper>
step5 写一个DAO接口,接口方法没有特定要求,本例没有做修改
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import entity.Employee;
import entity.NewEmployee;
/**
* Mapper映射器,接口方法不一定跟EmpMapper.xml中写的sql的id一致,本例中没有修改
* @author clyang
*/
public interface EmployeeDAO { //将一条数据插入数据库
public void save(Employee e);
//查询所有结果
public List<Employee> findAll();
//根据id来查找结果
public Employee findByID(int id);
//修改操作
public void modify(Employee e);
//删除操作
public void delete(int id);
//返回类型为map的查询,根据id来查询
public Map findOne(int id);
//当表的字段名和实体类的属性名不一致时,根据id来查询
public NewEmployee findOneByNewEmp(int id); }
step6 再写一个实现类实现这个接口,在接口的实现方法里写具体的方法,在里面使用注入的SqlSessionTemplate,调用其API,实现跟映射文件中sql的关联。
SqlSessionTemplate其对SqlSession进行了封装,为线程安全接口
import java.util.List;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import entity.Employee;
import entity.NewEmployee;
@Repository("empDAO")
public class EmployeeDAOImpl implements EmployeeDAO{ //注入SqlsessionTemplate
@Autowired
@Qualifier("sst")
private SqlSessionTemplate sst;
/**
* 不用考虑提交事务和关闭SqlSession,SqlSessionTemplate为线程安全接口
*/
public void save(Employee e) {
sst.insert("newname.save",e);
} public List<Employee> findAll() {
return sst.selectList("newname.findAll");
} public Employee findByID(int id) {
return sst.selectOne("newname.findByID", id);
} public void modify(Employee e) {
sst.update("newname.modify", e);
} public void delete(int id) {
sst.delete("newname.delete",id);
} public Map findOne(int id) {
return sst.selectOne("newname.findOne",id);
} public NewEmployee findOneByNewEmp(int id) {
return sst.selectOne("newname.findOneByNewEmp",id);
}
}
看大这里,就明白为什么不需要接口方法跟映射文件中的sql的id名字一样,命名空间也不做要求了,因为实现类方法中已经指明了。
测试代码:
import java.util.List; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.EmployeeDAO;
import entity.Employee; public class testCase {
@Test
public void test1() {
//启动spring容器
String config="spring-mybatis.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(config);
//调用EmployeeDAOImpl实现类,使用父类接口指向子类对象
EmployeeDAO dao=ac.getBean("empDAO",EmployeeDAO.class);
//调用查询方法
List<Employee> list=dao.findAll();
System.out.println(list);
//不需要手动关闭连接
}
}
测试结果:OK

总结:使用SqlSessionTemplate跟使用Mapper映射器配置方法整体区别不大,为早期的使用方法,主要为需要有实现类,实现类要依赖注入SqlSessionTemplate,然后使用它的API实现对数据库的操作。
Spring集成MyBatis的使用-使用SqlSessionTemplate的更多相关文章
- Spring集成Mybatis,spring4.x整合Mybatis3.x
Spring集成Mybatis,spring4.x整合Mybatis3.x ============================== 蕃薯耀 2018年3月14日 http://www.cnblo ...
- Unit08: Spring集成mybatis
Unit08: Spring集成mybatis 1. Spring集成mybatis (1)方式一 step1. 导包. spring-webmvc,mybatis,mybatis-spring, o ...
- Spring集成MyBatis框架
Java在写数据库查询时,我接触过四种方式: 1.纯Java代码,引用对应的数据库驱动包,自己写连接与释放逻辑(可以用连接池) 这种模式实际上性能是非常不错的,但是使用起来并不是非常方便:一是要手工为 ...
- SSM框架开发web项目系列(五) Spring集成MyBatis
前言 在前面的MyBatis部分内容中,我们已经可以独立的基于MyBatis构建一个数据库访问层应用,但是在实际的项目开发中,我们的程序不会这么简单,层次也更加复杂,除了这里说到的持久层,还有业务逻辑 ...
- Spring集成MyBatis的使用-使用Mapper映射器
Spring集成MyBatis使用 前面复习MyBatis时,发现在测试时,需要手动创建sqlSessionFactory,Spring将帮忙自动创建sqlSessionFactory,并且将自动扫描 ...
- spring集成mybatis配置多个数据源,通过aop自动切换
spring集成mybatis,配置多个数据源并自动切换. spring-mybatis.xml如下: <?xml version="1.0" encoding=" ...
- MyBatis从入门到精通(第9章):Spring集成MyBatis(下)
MyBatis从入门到精通(第9章):Spring集成MyBatis(下) springmvc执行流程原理 mybatis-spring 可以帮助我们将MyBatis代码无缝整合到Spring中.使 ...
- MyBatis从入门到精通(第9章):Spring集成MyBatis(中)
MyBatis从入门到精通(第9章):Spring集成MyBatis(中) 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法.应该将应用自身的设计和具体 ...
- MyBatis从入门到精通(第9章):Spring集成MyBatis(上)
MyBatis从入门到精通(第9章):Spring集成MyBatis(上) Spring是一个为了解决企业级Web应用开发过程中面临的复杂性,而被创建的一个非常流行的轻量级框架. mybatis-sp ...
随机推荐
- HTTP中Get、Post、Put与Delete。了解一下!
1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影响资源的内容,即该请求不会产生副作用.无论进行多少次操 ...
- 【Eclipse】如何在Eclipse中如何自动添加注释和自定义注释风格
背景简介 丰富的注释和良好的代码规范,对于代码的阅读性和可维护性起着至关重要的作用.几乎每个公司对这的要求还是比较严格的,往往会形成自己的一套编码规范.但是再实施过程中,如果全靠手动完成,不仅效率低下 ...
- JDK源码阅读顺序
很多java开发的小伙伴都会阅读jdk源码,然而确不知道应该从哪读起.以下为小编整理的通常所需阅读的源码范围. 标题为包名,后面序号为优先级1-4,优先级递减 1.java.lang 1) Obj ...
- Nodejs总结(一)
Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台,事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常 ...
- 理解 with递归调用 Sqlserver 树查询
--with用法 --可以这么理解 with SQL语句变量或者叫临时表名 as( SQL语句 ) select * from SQL语句变量或者叫临时表名 --递归调用 with CTE as( s ...
- spring controller 获取context
想要获取context需要先熟悉spring是怎么在web容器中启动的,spring启动过程其实就是其IOC容器的启动过程,对于web程序,IOC容器启动过程即是建立上下文的过程 spring启动过程 ...
- 《算法》第一章部分程序 part 1
▶ 书中第一章部分程序,加上自己补充的代码,包括若干种二分搜索,寻找图上连通分量数的两种算法 ● 代码,二分搜索 package package01; import java.util.Arrays; ...
- python中的partition、rpartition
partition()从左向右寻找,以字符串中的某个元素为中心将左右分割共分割成三个元素并放入到元组中 partition()从右向左寻找,以字符串中的某个元素为中心将左右分割共分割成三个元素并放入到 ...
- faceted project validation builder
Should I keep Eclipse Java facet? Facets automate some parts of project configuration and deployment ...
- <转载> nginx服务器安装及配置文件详解 https://segmentfault.com/a/1190000002797601
nginx在工作中已经有好几个环境在使用了,每次都是重新去网上扒博客,各种编译配置,今天自己也整理一份安装文档和nginx.conf配置选项的说明,留作以后参考.像负载均衡配置(包括健康检查).缓存( ...