Spring MVC+MyBatis框架使用多个数据源以及之间的切换

longdan0 2015-11-26

至于怎么搭建Spring MVC+MyBatis框架,我这里就不详细说了,可以参考:http://xieke90.iteye.com/blog/2241054

一、同时使用多个数据源

   以下实例演示同时使用两个数据源:

   <1>、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:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       default-init-method="init">    
     
    <!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    <!--创建jdbc数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>
    <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url2}" />
        <property name="username" value="${username2}" />
        <property name="password" value="${password2}" />
    </bean>
 
    <!-- 创建SqlSessionFactory,同时指定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
         <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
        <property name="mapperLocations" value="classpath:sqlmap/car/*.xml" />
    </bean>
    <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource2" />
        <property name="mapperLocations" value="classpath:sqlmap/order/*.xml" />
    </bean>
 
    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xieke.test.mapper.car" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xieke.test.mapper.order" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
    </bean>
    
</beans>

   <2>、jdbc.properties

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/test
username=root
password=999999

url2=jdbc:mysql://localhost:3306/test2
username2=root
password2=999999

   <3>、以上是主要的配置,其它配置跟配置Spring MVC是一样的,详细情况请阅读下面的源代码。

二、数据源之间的切换

   以下演示两个数据源之间的切换:

   <1>、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:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       default-init-method="init">    
     
    <!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    <!--创建jdbc数据源 -->
    <bean id="dataSourceA" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${a.url}" />
        <property name="username" value="${a.username}" />
        <property name="password" value="${a.password}" />
    </bean>
    <bean id="dataSourceB" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${b.url}" />
        <property name="username" value="${b.username}" />
        <property name="password" value="${b.password}" />
    </bean>
    
    <!-- 配置dataSource管理key和value的对应,默认选择dataSourceB -->
    <bean id="dataSource" class="com.xieke.test.util.DynamicDataSource">
        <!-- 通过key-value的形式来关联数据源 -->
        <property name="targetDataSources">
                <map key-type="java.lang.String">
                        <entry value-ref="dataSourceA" key="dataSourceA"></entry>
                        <entry value-ref="dataSourceB" key="dataSourceB"></entry>
                </map>
        </property>
        <property name="defaultTargetDataSource" ref="dataSourceB" >
        </property>
    </bean>
 
    <!-- 创建SqlSessionFactory,同时指定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
         <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
        <property name="mapperLocations" value="classpath:sqlmap/**/*.xml" />
    </bean>

    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xieke.test.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
    </bean>
    
</beans>

   <2>、jdbc.properties

driver=com.mysql.jdbc.Driver

a.url=jdbc:mysql://localhost:3306/test
a.username=root
a.password=999999

b.url=jdbc:mysql://localhost:3306/test2
b.username=root
b.password=999999

   <3>、数据源管理类:DynamicDataSource,需要继承AbstractRoutingDataSource。

package com.xieke.test.util;

import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource
{
    public static final String DATA_SOURCE_A = "dataSourceA";
    public static final String DATA_SOURCE_B = "dataSourceB";
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

    public static void setCustomerType(String customerType)
    {
        contextHolder.set(customerType);
    }

    public static String getCustomerType()
    {
        return contextHolder.get();
    }

    public static void clearCustomerType()
    {
        contextHolder.remove();
    }

    @Override
    protected Object determineCurrentLookupKey()
    {
        return getCustomerType();
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException
    {
        return null;
    }
}

   <4>、切换数据源时调用setCustomerType方法,比如我要使用dataSourceA,切换代码就是“DynamicDataSource.setCustomerType(DynamicDataSource.DATA_SOURCE_A);”。

   <5>、以上是主要的配置,其它配置跟配置Spring MVC是一样的,详细情况请阅读下面的源代码。

三、源代码下载地址

  <1>、同时使用两个数据源实例的源代码下载地址:http://pan.baidu.com/s/1o66a3kI

  <2>、两个数据源之间的切换实例的源代码下载地址:http://pan.baidu.com/s/1sjxgPSp

  转载请注明出处:http://xieke90.iteye.com/blog/2259320

相关推荐