Spring 与 MyBatis 的整合
web.xml 环境配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicactionContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
maven 导包:
<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>maven</groupId>
<artifactId>myBatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- 定义 Spring 的版本号 -->
<springframework.version>3.2.4.RELEASE</springframework.version>
</properties>
<dependencies>
<!-- Oracle 数据库的 JDBC 驱动,商业组件,非开源,需要自己注册到 maven 仓库 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<!-- dbcp 数据源(连接池) ,必须 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- MyBatis 核心,必须 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.5</version>
</dependency>
<!-- MyBatis 与 Spring 整合包 ,必须,整合 Spring 的关键 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<!-- Spring 核心包,必须 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring ORM,必须 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring 切面, 用于配置事务切面, 若使用注解配置事务, 不需要切面的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring 测试,可选,仅用于单元测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Junit 测试,可选,仅用于单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- JDBC 连接 properties 文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 依赖注入扫描范围 -->
<context:component-scan base-package="myBatis" />
<!-- 配置 dbcp 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="0"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="20"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="1"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000"></property>
</bean>
<!-- 配置 MyBatis 的 SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<property name="typeAliasesPackage" value="myBatis.entity" />
</bean>
<!-- 扫描 DAO 接口所在包名,Spring 会自动代理生成其下的接口实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="myBatis.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
mapper.xml 配置文件:
<?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="myBatis.dao.CategoryDao">
<resultMap type="myBatis.entity.Category" id="categoryResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
<select id="fetchById" parameterType="int" resultMap="categoryResultMap">
select * from Category where id=#{id}
</select>
<update id="update" parameterType="Category" >
update Category set name=#{ name } where id = #{id}
</update>
<insert id="add" parameterType="Category" >
insert into Category(ID,NAME) values(seq_cinema_cate.nextval, #{name})
</insert>
<select id="getAll" resultMap="categoryResultMap">
select m.*,c.name from Movie m inner join Category c on m.categoryId = c.id
</select>
</mapper>
对应的 数据层 接口代码:
package myBatis.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import myBatis.entity.Category;
@Repository("categoryDao")
public interface CategoryDao {
public Category fetchById(int id);
public void update(Category category);
public void add(Category category);
public List<Category> getAll();
}
服务层,或 Dao层 测试:
package myBatis.biz.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import myBatis.biz.CategoryBiz;
import myBatis.dao.CategoryDao;
import myBatis.entity.Category;
@Component("categoryBiz")
public class CategoryBizImpl implements CategoryBiz{
@Autowired
private CategoryDao categoryDao;
@Override
public Category fetchById(int id) {
return categoryDao.fetchById(id);
}
@Override
public int sum(int a , int b){
return a+b ;
}
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
CategoryBiz categoryBiz = (CategoryBiz)ac.getBean("categoryBiz");
CategoryDao categoryDao = (CategoryDao)ac.getBean("categoryDao");
System.out.println(categoryBiz.fetchById(2).getName());
System.out.println(categoryDao.fetchById(3).getId());
}
}
单元测试:
package myBatis;
import myBatis.dao.CategoryDao;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@ContextConfiguration("classpath:applicationContext.xml")
public class CategoryDaoTest extends AbstractJUnit4SpringContextTests {
@Autowired
public CategoryDao categoryDao;
@Test
public void testAll(){
System.out.println(categoryDao.fetchById(2));
}
}