MyBatis系列目录--7. Mybatis与Spring最佳整合

转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662


 

1. 整合思路

(1) SqlSessionFactory由Spring作为单例管理

(2) spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。

(3) 持久层mapper(dao)由spring管理

2. 依赖

(1)spring依赖

<spring.version>3.2.8.RELEASE</spring.version>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-beans</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-expression</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>${spring.version}</version>
</dependency>

(2)spring-mybatis依赖 

<mybatis.spring.version>1.2.2</mybatis.spring.version>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>${mybatis.spring.version}</version>
</dependency>

(3) c3p0 

<c3p0.version>0.9.2</c3p0.version>
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>${c3p0.version}</version>
</dependency>


3. spring配置:(spring配置文件放到classpath下spring目录下) 

(1)spring总配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
	default-autowire="byName">
	<import resource="classpath:spring/spring-data.xml" />
	<import resource="classpath:spring/spring-mybatis.xml" />
</beans>


(2)spring-data.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">
    <context:property-placeholder location="classpath:db.properties"/>
    <bean name="footballDB" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${football.driver}"/>
        <property name="jdbcUrl" value="${football.url}"/>
        <property name="user" value="${football.username}"/>
        <property name="password" value="${football.password}"/>
        <property name="initialPoolSize" value="10"/>
        <property name="maxPoolSize" value="30"/>
        <property name="testConnectionOnCheckin" value="false"/>
        <property name="testConnectionOnCheckout" value="true"/>
    </bean>
</beans>

(3)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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       default-autowire="byName">
    <bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="footballDB"/>
        <!-- mybatis配置文件的位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- domain的包路径,类似MyBatis的typeAliases配置 -->
        <property name="typeAliasesPackage" value="com.sohu.tv.bean"/>
        <!-- mapper配置文件的路径,类似MyBatis的mappers配置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!-- 扫描接口类的包路径 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sohu.tv.mapper"/>
    </bean>
</beans>


(4)mybatis-config.xml配置: 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--数据库的字段名到pojo类的属性名的自动映射-->
    <settings>
        <setting name="useColumnLabel" value="true"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

(5)db.properties: 

football.driver=com.mysql.jdbc.Driver
football.url=jdbc:mysql://localhost:3306/football
football.username=root
football.password=your password


4. 单元测试: 

spring基类测试

package com.sohu.tv.spring;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * spring-mybatis基类测试
 * @author leifu
 * @Date 2015年1月12日
 * @Time 下午2:34:21
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring.xml"})
public class MyBatisSpringBaseTest  extends Assert {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
}
package com.sohu.tv.spring;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import com.sohu.tv.bean.Club;
import com.sohu.tv.mapper.ClubDao;
/**
 * spring-mybatis clubDao测试
 * 
 * @author leifu
 * @Date 2015年8月4日
 * @Time 下午12:02:21
 */
public class MyBatisSpringClubTest extends MyBatisSpringBaseTest {
    @Resource(name = "clubDao")
    private ClubDao clubDao;
    @Test
    public void testGetAllClubs() {
        List<Club> clubList = clubDao.getAllClubs();
        if (clubList != null && !clubList.isEmpty()) {
            System.out.println("clubList size: " + clubList.size());
            for (Club club : clubList) {
                System.out.println(club);
            }
        }
    }
}

相关推荐