经常遇到需要在两个数据源之间来回切换,基于SSM配置步骤如下:
1、application-context.xml中:
(1)一个数据源时的配置:
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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-3.0.xsd">
<!-- 数据源定义 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 数据源定义 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${connection.driverClassName}" />
<property name="url" value="${connection.url}" />
<property name="username" value="${connection.username}" />
<property name="password" value="${connection.password}" />
<property name="maxActive" value="${connection.maxActive}" />
<property name="maxIdle" value="${connection.maxIdle}" />
<property name="minIdle" value="${connection.minIdle}" />
<property name="removeAbandoned" value="${connection.removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${connection.removeAbandonedTimeout}" />
<property name="logAbandoned" value="${connection.logAbandoned}" />
<property name="defaultAutoCommit" value="${connection.defaultAutoCommit}" />
<property name="defaultReadOnly" value="${connection.defaultReadOnly}" />
<property name="validationQuery" value="${connection.validationQuery}" />
<property name="testOnBorrow" value="${connection.testOnBorrow}" />
</bean>
<!-- 创建sqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 自动扫描mapper目录, 省掉mybatis-config.xml里的手工配置 -->
<property name="mapperLocations">
<list>
<!-- <value>classpath:com/loan/*/dao/xml/*.xml</value> -->
<value>classpath:com/loan/fore/dao/xml/*.xml</value>
<value>classpath:com/loan/back/dao/xml/*.xml</value>
<value>classpath:com/loan/security/dao/xml/*.xml</value>
</list>
</property>
</bean>
<!-- 通过扫描的模式,扫描目录在com/loan/mapper目录下 -->
<be