Spring集成MyBatis的方式
使用Mapper映射器
导包
<dependencies>
<!--springmvc依赖的jar包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- mybatis的jar包 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- mybatis和spring整合的jar包 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!--springjdbc的jar包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- mysql的jar包 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- junit单元测试 -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--数据库连接池 dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
添加Spring配置文件
MyBatis配置文件的内容变成了一个bean (SqlSessionFactoryBean)
<?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">
<context:property-placeholder
location="classpath:db.properties" />
<!-- 配置数据库连接池 -->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="ds" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<!-- 该bean的作用是用来代替MyBatis配置文件 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean"
id="sqlSessionFactory">
<!-- 指定连接池 -->
<property name="dataSource" ref="ds" />
<!-- 指定映射文件位置 -->
<property name="mapperLocations"
value="classpath:entity/*.xml" />
</bean>
<!-- 配置MapperScannerConfigurer -->
<!--
该bean会扫描指定包及其子包下面的所有的Mapper
映射器(接口),然后生成符合该接口要求的对象
(通过调用SqlSession的getMapper方法),接下来,
会将这些对象(即Mapper映射器的实现对象)添加到
Spring容器里面(默认的id是首字母小写之后的接口名)。
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"/>
</bean>
</beans>
实体类
package entity;
public class Employee {
private int id;
private String ename;
private double salary;
private int age;
public String toString() {
return "Employee [id=" + id + ", ename=" + ename + ", salary=" + salary + ", age=" + age + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package entity;
public class Emp2 {
private int empNo;
private String name;
private double salary;
private int age;
public String toString() {
return "Emp2 [empNo=" + empNo + ", name=" + name + ", salary=" + salary + ", age=" + age + "]";
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
映射文件
<?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="dao.EmployeeDAO">
<!--
id:每一条sql语句都有唯一的id,称之为sqlId。
parameterType:参数类型(如果是类,要写全限定名)
-->
<insert id="save" parameterType="entity.Employee">
INSERT INTO t_emp VALUES(null,
#{ename},#{salary},#{age})
</insert>
<!--
resultType: 返回类型(如果是类,要写全限定名)
-->
<select id="findAll"
resultType="entity.Employee">
SELECT * FROM t_emp
</select>
<select id="findById" parameterType="int"
resultType="entity.Employee">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
<update id="update"
parameterType="entity.Employee">
UPDATE t_emp SET ename = #{ename},
salary = #{salary},age = #{age}
WHERE id = #{id}
</update>
<delete id="delete" parameterType="int">
DELETE FROM t_emp WHERE id = #{id1}
</delete>
<!-- 返回Map类型的结果 -->
<!--
map是java.util.Map的简写形式。
-->
<select id="findById2" parameterType="int"
resultType="map">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
<!--
设置字段名与实体类的属性名的对应关系。
注:
只需要将不一致的列出来。
-->
<resultMap type="entity.Emp2" id="empResultMap">
<result property="empNo" column="id"/>
<result property="name" column="ename"/>
</resultMap>
<select id="findById3" parameterType="int"
resultMap="empResultMap">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
</mapper>
Mapper映射器
package dao;
import java.util.List;
import java.util.Map;
import entity.Employee;
/**
* Mapper映射器
*
*/
public interface EmployeeDAO {
public void save(Employee emp);
public List<Employee> findAll();
public Employee findById(int id);
public void update(Employee e);
public void delete(int id);
public Map findById2(int id);
}
配置MapperScannerConfigurer
该bean会扫描指定包及其子包下面的所有的 Mapper映射器(接口),
会调用SqlSession的getMapper方法返回Mapper映射器的实现对象,
并且将这些对象添加到Spring容器里面。
(默认的id是首字母小写之后的接口名)
在spring和mybatis整合的配置文件spring-mybatis.xml中添加如下配置
<!-- 配置MapperScannerConfigurer -->
<!--
该bean会扫描指定包及其子包下面的所有的Mapper
映射器(接口),然后生成符合该接口要求的对象
(通过调用SqlSession的getMapper方法),接下来,
会将这些对象(即Mapper映射器的实现对象)添加到
Spring容器里面(默认的id是首字母小写之后的接口名)。
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"/>
</bean>
测试程序
package test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.EmployeeDAO;
import entity.Employee;
public class TestCase {
private EmployeeDAO dao;
@Before
public void before() {
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-mybatis.xml");
dao=ac.getBean("employeeDAO",EmployeeDAO.class);
}
@Test
public void save() {
Employee e=new Employee();
e.setEname("王老师");
e.setSalary(20000);
e.setAge(18);
dao.save(e);
}
}
只扫描带有特定注解的Mapper映射器
- 开发一个注解
- 将注解添加到Mapper映射器上面
- 配置MapperScannerConfigurer
依赖的jar包
<dependencies>
<!--导入junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<classifier>javadoc</classifier>
</dependency>
<!-- 导入数据库包 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- 导入mybatis的包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
<classifier>javadoc</classifier>
</dependency>
<!-- 针对mysql的导包 -->
<dependency>
<groupId>org.wisdom-framework</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34_1</version>
</dependency>
<!-- 导入spring-webmvc的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.3.RELEASE</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.3.RELEASE</version>
<classifier>javadoc</classifier>
</dependency>
<!-- 针对spring JDBC导包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.3.RELEASE</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.3.RELEASE</version>
<classifier>javadoc</classifier>
</dependency>
<!-- 关键:导入mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
<classifier>javadoc</classifier>
</dependency>
</dependencies>
spring和mybatis整合的配置文件
<?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">
<context:property-placeholder
location="classpath:db.properties" />
<!-- 配置数据库连接池 -->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="ds" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<!-- 该bean的作用是用来代替MyBatis配置文件 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean"
id="sqlSessionFactory">
<!-- 指定连接池 -->
<property name="dataSource" ref="ds" />
<!-- 指定映射文件位置 -->
<property name="mapperLocations"
value="classpath:entity/*.xml" />
</bean>
<!-- 配置MapperScannerConfigurer -->
<!--
该bean会扫描指定包及其子包下面的所有的Mapper
映射器(接口),然后生成符合该接口要求的对象
(通过调用SqlSession的getMapper方法),接下来,
会将这些对象(即Mapper映射器的实现对象)添加到
Spring容器里面(默认的id是首字母小写之后的接口名)。
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包 -->
<property name="basePackage" value="dao" />
<!-- 指定只扫描带有该注解的接口 -->
<property name="annotationClass"
value="annotations.MyBatisRepository" />
</bean>
</beans>
数据库连接配置db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ems?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
实体类
package entity;
public class Employee {
private int id;
private String ename;
private double salary;
private int age;
public String toString() {
return "Employee [id=" + id + ", ename=" + ename + ", salary=" + salary + ", age=" + age + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
映射文件
<?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="dao.EmployeeDAO">
<!--
id:每一条sql语句都有唯一的id,称之为sqlId。
parameterType:参数类型(如果是类,要写全限定名)
-->
<insert id="save" parameterType="entity.Employee" >
INSERT INTO t_emp VALUES(null,
#{ename},#{salary},#{age})
</insert>
<!--
resultType: 返回类型(如果是类,要写全限定名)
-->
<select id="findAll"
resultType="entity.Employee">
SELECT * FROM t_emp
</select>
<select id="findById" parameterType="int"
resultType="entity.Employee">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
<update id="update"
parameterType="entity.Employee">
UPDATE t_emp SET ename = #{ename},
salary = #{salary},age = #{age}
WHERE id = #{id}
</update>
<delete id="delete" parameterType="int">
DELETE FROM t_emp WHERE id = #{id1}
</delete>
<!-- 返回Map类型的结果 -->
<!--
map是java.util.Map的简写形式。
-->
<select id="findById2" parameterType="int"
resultType="map">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
<!--
设置字段名与实体类的属性名的对应关系。
注:
只需要将不一致的列出来。
-->
<resultMap type="entity.Emp2" id="empResultMap">
<result property="empNo" column="id"/>
<result property="name" column="ename"/>
</resultMap>
<select id="findById3" parameterType="int"
resultMap="empResultMap">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
</mapper>
数据库访问层
package dao;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import annotations.MyBatisRepository;
import entity.Employee;
/**
* Mapper映射器
*
*/
@Repository
@MyBatisRepository
public interface EmployeeDAO {
public void save(Employee emp);
public List<Employee> findAll();
public Employee findById(int id);
public void update(Employee e);
public void delete(int id);
public Map findById2(int id);
}
自定义注解
package annotations;
public @interface MyBatisRepository {
}
测试程序
package test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.EmployeeDAO;
import entity.Employee;
public class TestCase {
private EmployeeDAO dao;
@Before
public void before() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mybatis.xml");
dao = ac.getBean("employeeDAO", EmployeeDAO.class);
}
@Test
public void save() {
Employee e = new Employee();
e.setEname("里老师");
e.setSalary(20000);
e.setAge(18);
dao.save(e);
}
@Test
public void findAll() {
List<Employee> employees = dao.findAll();
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
不使用Mapper映射器
实现步骤
- 导包spring-webmvc,mybatis,mybatis-spring,spring-jdbc,mysql,dbcp,junit
- 添加Spring配置文件,MyBatis配置文件的内容变成了一个bean (SqlSessionFactoryBean)
- 实体类
- 映射文件,namespace可以自定义
- DAO接口,方法名等不做要求
- DAO实现类
- 配置SqlSessionTemplate,可以将SqlSessionTemplate注入到DAO,然后调用SqlSessionTemplate提供的方法即可。SqlSessionTemplate封装了SqlSession,我们不用去考虑如何获得SqlSession,如何关闭
SqlSession。
jar包的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
数据库连接配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ems?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
spring和mybatis整合的配置文件
<?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">
<context:property-placeholder
location="classpath:db.properties" />
<!--数据库连接池的配置 -->
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="ds">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<!--
该bean的作用是用来代替MyBatis配置文件
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean"
id="sqlSessionFactory">
<!-- 指定连接池 -->
<property name="dataSource" ref="ds" />
<!-- 指定映射文件位置 -->
<property name="mapperLocations"
value="classpath:entity/*.xml" />
</bean>
<!-- 配置SqlSessionTemplate -->
<!--
SqlSessionTemplate封装了SqlSession的操作。
-->
<bean id="st" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 指定SqlSessionFactoryBean -->
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!--组件扫描 -->
<context:component-scan base-package="dao" />
</beans>
实体类
package entity;
public class Employee {
private int id;
private String ename;
private double salary;
private int age;
public String toString() {
return "Employee [id=" + id + ", ename=" + ename + ", salary=" + salary + ", age=" + age + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package entity;
public class Emp2 {
private int empNo;
private String name;
private double salary;
private int age;
public String toString() {
return "Emp2 [empNo=" + empNo + ", name=" + name + ", salary=" + salary + ", age=" + age + "]";
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
映射文件
<?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="dao.EmployeeDAO">
<!--
id:每一条sql语句都有唯一的id,称之为sqlId。
parameterType:参数类型(如果是类,要写全限定名)
-->
<insert id="save" parameterType="entity.Employee">
INSERT INTO t_emp VALUES(null,
#{ename},#{salary},#{age})
</insert>
<!--
resultType: 返回类型(如果是类,要写全限定名)
-->
<select id="findAll"
resultType="entity.Employee">
SELECT * FROM t_emp
</select>
<select id="findById" parameterType="int"
resultType="entity.Employee">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
<update id="update"
parameterType="entity.Employee">
UPDATE t_emp SET ename = #{ename},
salary = #{salary},age = #{age}
WHERE id = #{id}
</update>
<delete id="delete" parameterType="int">
DELETE FROM t_emp WHERE id = #{id1}
</delete>
<!-- 返回Map类型的结果 -->
<!--
map是java.util.Map的简写形式。
-->
<select id="findById2" parameterType="int"
resultType="map">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
<!--
设置字段名与实体类的属性名的对应关系。
注:
只需要将不一致的列出来。
-->
<resultMap type="entity.Emp2" id="empResultMap">
<result property="empNo" column="id"/>
<result property="name" column="ename"/>
</resultMap>
<select id="findById3" parameterType="int"
resultMap="empResultMap">
SELECT * FROM t_emp WHERE id = #{id1}
</select>
</mapper>
持久层接口
package dao;
import java.util.List;
import java.util.Map;
import entity.Emp2;
import entity.Employee;
/**
* DAO接口
*
*/
public interface EmployeeDAO {
public void save(Employee emp);
public List<Employee> findAll();
public Employee findById(int id);
public void update(Employee e);
public void delete(int id);
public Map findById2(int id);
public Emp2 findById3(int id);
}
持久层接口的实现类
package dao;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import entity.Emp2;
import entity.Employee;
@Repository("empDao")
public class EmployeeDAOMyBatisImpl implements EmployeeDAO {
@Resource(name = "st")
SqlSessionTemplate st;
/*
* 不用考虑提交事务和关闭SqlSession, SqlSessionTemplate已经处理了。
*/
public void save(Employee emp) {
st.insert("dao.EmployeeDAO.save", emp);
}
public List<Employee> findAll() {
return st.selectList("dao.EmployeeDAO.findAll");
}
public Employee findById(int id) {
return st.selectOne("dao.EmployeeDAO.findById", id);
}
public void update(Employee e) {
st.update("dao.EmployeeDAO.update", e);
}
public void delete(int id) {
st.delete("dao.EmployeeDAO.delete", id);
}
public Map findById2(int id) {
return st.selectOne("dao.EmployeeDAO.findById2", id);
}
public Emp2 findById3(int id) {
return st.selectOne("dao.EmployeeDAO.findById3", id);
}
}
测试程序
package test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.EmployeeDAO;
import dao.EmployeeDAOMyBatisImpl;
import entity.Employee;
public class TestCase {
EmployeeDAO dao;
@Before
public void before() {
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-mybatis.xml");
dao=ac.getBean("empDao",EmployeeDAOMyBatisImpl.class);
}
@Test
public void save() {
Employee emp=new Employee();
emp.setEname("李老师");
emp.setAge(20);
emp.setSalary(5000);
dao.save(emp);
}
@Test
public void findAll() {
List<Employee> findAll=dao.findAll();
for (Employee employee : findAll) {
System.out.println(employee);
}
}
}
注意
由于spring 3兼容jdk 7, spring 4兼容jdk 8,此测试程序的运行环境是jdk1.7,jdk1.8无法实现测试程序