多数据源配置方法:
在配置数据源配置文件中多加一个数据源配置即可:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url2}" />
<property name="username" value="${username2}" />
<property name="password" value="${password2}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
上面是两个数据源;
<!-- 配置 mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 mybatis2 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 BaseDao -->
<bean id="baseDao" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 配置 BaseDao2 -->
<bean id="baseDao2" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory2"/>
</bean>
想配几个就加几个数据源,
测试类:
@Autowired
private BaseDao baseDao;
@Resource(name="baseDao2")
private BaseDao baseDao2;
@Scheduled(cron = "0/30 * * * * ?")
public void TestTwoDataSource(){
System.out.println("sssssssss");
Map<String,String> data1 = new HashMap<String,String>();
data1= baseDao.selectOne(NameSpace.BS_QUESTION_INFOMapper, "findRouteConfigTest");
System.out.println("dara1:"+data1);
Map<String,String> data2 = new HashMap<String,String>();
data2= baseDao2.selectOne(NameSpace.BS_QUESTION_INFOMapper, "findRouteConfigTest");
System.out.println("data2:"+data2);
}
完整数据源配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<tx:annotation-driven />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/env/jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url2}" />
<property name="username" value="${username2}" />
<property name="password" value="${password2}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
<!-- 配置 mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 mybatis2 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 BaseDao -->
<bean id="baseDao" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 配置 BaseDao2 -->
<bean id="baseDao2" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory2"/>
</bean>
<!-- Mybatis Dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sitech.message.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
name:与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*'、'handle*'、'on*Event'等等。
propagation="REQUIRED":事务传播行为
isolation 默认值 DEFAULT:事务隔离级别
timeout 默认值 -1 事务超时的时间(以秒为单位)
read-only 默认值 false 事务是否只读?
rollback-for 将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
no-rollback-for 不被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="batch*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!--
把事务控制在Service层
第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.polin.omeal.service下的任意class
第三个 * —— 通配 包com.polin.omeal.service下的任意class的任意方法
第四个 .. —— 通配 方法可以有0个或多个参数
综上:包com.polin.omeal.service下的任意class的具有任意返回值类型、任意类、任意名称的方法、任意数目参数和<tx:advice/>有关的设置
-->
<aop:config>
<aop:pointcut id="aop" expression="execution(* com.sitech.message.*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="aop"/>
</aop:config>
</beans>