您的位置:首页 > 编程语言 > Java开发

spring多数据源配置

2015-06-26 11:26 471 查看
转:http://blog.csdn.net/wangpeng047/article/details/8866239

多数据源问题,目的,可以使用xml来自由配置切换的数据源

spring-mybatis.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">

<!--配置jdbc.properties文件的位置信息,路径还是区分大小写 -->

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:config.properties</value>
</list>
</property>
</bean>

<!-- 数据库数据源1 -->
<bean id="dataSourceOne" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dbOne.jdbc.driver}" />
<property name="url" value="${dbOne.jdbc.url}" />
<property name="username" value="${dbOne.jdbc.user}" />
<property name="password" value="${dbOne.jdbc.password}" />
<!-- 数据库连接池配置 -->
<property name="initialSize" value="10" /><!-- 初始化连接数量 -->
<property name="maxActive" value="100" /><!-- 最大连接数量 -->
<property name="maxIdle" value="50" /><!-- 最大空闲连接数量 -->
<property name="minIdle" value="10" /><!-- 最小空闲连接数量 -->
</bean>

<!-- 数据库数据源2 -->
<bean id="dataSourceTwo" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dbTwo.jdbc.driver}" />
<property name="url" value="${dbTwo.jdbc.url}" />
<property name="username" value="${dbTwo.jdbc.user}" />
<property name="password" value="${dbTwo.jdbc.password}" />
<!-- 数据库连接池配置 -->
<property name="initialSize" value="10" /><!-- 初始化连接数量 -->
<property name="maxActive" value="100" /><!-- 最大连接数量 -->
<property name="maxIdle" value="50" /><!-- 最大空闲连接数量 -->
<property name="minIdle" value="10" /><!-- 最小空闲连接数量 -->
</bean>

<bean id="dataSource" class="com.***.aop.DynamicDataSource">
<property name="defaultTargetDataSource" ref="dataSourceOne" />
<property name="targetDataSources">
<map>
<entry key="dataSourceOne" value-ref="dataSourceOne" />
<entry key="dataSourceTwo" value-ref="dataSourceTwo" />
</map>
</property>
</bean>

<!-- MyBatis在spring中Bean的配置,都是固定的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatisConfig.xml" />
</bean>
<bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

<!-- 事务管理 -->
<!-- 事务注解驱动,标注@Transactional的类和方法将具有事务性 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>


2. DynamicDataSource.class

package com.xxx.aop;

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

public class DynamicDataSource extends AbstractRoutingDataSource{

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

}


3. DatabaseContextHolder.class

package com.xxx.aop;

public class DatabaseContextHolder {

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();
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: