SqlSessionTemplate使用:
- 在使用Mybatis与Spring集成的时候我们会用到SqlSessionTemplate 这个类。
- 具体如何使用以及如何在SpringMVC中使用呢?
通过简单案例演示如下:
案例演示:
工程案例目录结构
数据库表结构:
导入需要的jar 或配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study</groupId>
<artifactId>springcase-day08</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
Cost.java
package com.dk.entity;
import java.io.Serializable;
import java.sql.Date;
public class Cost implements Serializable{
private Integer cost_id;
private String name;
private Long base_duration;
private Double base_cost;
private Double uint_cost;
private String status;
private String descr;
private Date creatime;
private Date startime;
private String cost_type;
public Integer getCost_id() {
return cost_id;
}
public void setCost_id(Integer cost_id) {
this.cost_id = cost_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getBase_duration() {
return base_duration;
}
public void setBase_duration(Long base_duration) {
this.base_duration = base_duration;
}
public Double getBase_cost() {
return base_cost;
}
public void setBase_cost(Double base_cost) {
this.base_cost = base_cost;
}
public Double getUint_cost() {
return uint_cost;
}
public void setUint_cost(Double uint_cost) {
this.uint_cost = uint_cost;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
public Date getCreatime() {
return creatime;
}
public void setCreatime(Date creatime) {
this.creatime = creatime;
}
public Date getStartime() {
return startime;
}
public void setStartime(Date startime) {
this.startime = startime;
}
public String getCost_type() {
return cost_type;
}
public void setCost_type(String cost_type) {
this.cost_type = cost_type;
}
}
CostDAO.java
package com.dk.dao;
import java.util.List;
import com.dk.entity.Cost;
public interface CostDAO {
public List<Cost> findAll();
}
CostDAOImpl.java
package com.dk.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import com.dk.dao.CostDAO;
import com.dk.entity.Cost;
/**
* 在springMVC中使用方式
* @author Cher_du
*
*/
@Repository("costDAOImpl")
public class CostDAOImpl implements CostDAO{
@Resource(name="sqlSession")
private SqlSessionTemplate template;;
public List<Cost> findAll() {
return template.selectList("findAll");
}
}
applicationContext.xml
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- <property name="sqlSessionFactory" ref="ssf"></property> -->
<constructor-arg index="0" ref="ssf"></constructor-arg>
</bean>
<!-- 创建DataSource对象 -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="learn"></property>
<property name="password" value="learn"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
</bean>
<!-- 定义SqlSessionFactoryBean ,
创建SqlSessionFactory对象 -->
<bean id="ssf"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接参数 -->
<property name="dataSource" ref="dbcp"></property>
<!-- SQL定义文件参数 -->
<property name="mapperLocations" value="classpath:mapper/*.xml">
</property>
</bean>
<!-- 根据Mapper接口生成实现对象,
默认接口名首字母小写做id属性值 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描Mapper接口的路径 -->
<property name="basePackage" value="com.dk.dao"></property>
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>
<!-- <bean id="costDao"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.dk.dao.CostDAO"></property>
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>
-->
</beans>
CostMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.dk.dao.CostDAO">
<select id="findAll" resultType="com.dk.entity.Cost">
select * from cost
</select>
</mapper>
TestCostDAO.java
package test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dk.dao.CostDAO;
import com.dk.entity.Cost;
public class TestCostDAO {
@Test
public void test(){
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
System.out.println(ac);
SqlSessionTemplate T= ac.getBean("sqlSession", SqlSessionTemplate.class);
System.out.println(T);
List<Cost> list= T.selectList("findAll");
for(Cost cost:list){
System.out.println(cost.getName());
}
}
}
运行test进行测试控制台输出: