码迷,mamicode.com
首页 > 编程语言 > 详细

spring多数据源配置

时间:2015-06-26 12:30:54      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

转:http://blog.csdn.net/wangpeng047/article/details/8866239

 

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

 

spring-mybatis.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc 
 7                         http://www.springframework.org/schema/mvc/spring-mvc.xsd
 8                         http://www.springframework.org/schema/beans 
 9                         http://www.springframework.org/schema/beans/spring-beans.xsd 
10                         http://www.springframework.org/schema/context 
11                         http://www.springframework.org/schema/context/spring-context.xsd
12                         http://www.springframework.org/schema/tx 
13                         http://www.springframework.org/schema/tx/spring-tx.xsd"
14     xmlns:tx="http://www.springframework.org/schema/tx">
15 
16     <!--配置jdbc.properties文件的位置信息,路径还是区分大小写 -->
17 
18     <bean
19         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
20         <property name="locations">
21             <list>
22                 <value>classpath:jdbc.properties</value>
23                 <value>classpath:config.properties</value>
24             </list>
25         </property>
26     </bean>
27     
28     <!-- 数据库数据源1 -->
29     <bean id="dataSourceOne" class="org.apache.commons.dbcp.BasicDataSource"
30         destroy-method="close">
31         <property name="driverClassName" value="${dbOne.jdbc.driver}" />
32         <property name="url" value="${dbOne.jdbc.url}" />
33         <property name="username" value="${dbOne.jdbc.user}" />
34         <property name="password" value="${dbOne.jdbc.password}" />
35         <!-- 数据库连接池配置 -->
36         <property name="initialSize" value="10" /><!-- 初始化连接数量 -->
37         <property name="maxActive" value="100" /><!-- 最大连接数量 -->
38         <property name="maxIdle" value="50" /><!-- 最大空闲连接数量 -->
39         <property name="minIdle" value="10" /><!-- 最小空闲连接数量 -->
40     </bean>
41     
42     <!-- 数据库数据源2 -->
43     <bean id="dataSourceTwo" class="org.apache.commons.dbcp.BasicDataSource"
44         destroy-method="close">
45         <property name="driverClassName" value="${dbTwo.jdbc.driver}" />
46         <property name="url" value="${dbTwo.jdbc.url}" />
47         <property name="username" value="${dbTwo.jdbc.user}" />
48         <property name="password" value="${dbTwo.jdbc.password}" />
49         <!-- 数据库连接池配置 -->
50         <property name="initialSize" value="10" /><!-- 初始化连接数量 -->
51         <property name="maxActive" value="100" /><!-- 最大连接数量 -->
52         <property name="maxIdle" value="50" /><!-- 最大空闲连接数量 -->
53         <property name="minIdle" value="10" /><!-- 最小空闲连接数量 -->
54     </bean>
55     
56     <bean id="dataSource" class="com.***.aop.DynamicDataSource">
57         <property name="defaultTargetDataSource" ref="dataSourceOne" />
58         <property name="targetDataSources">
59             <map>
60                 <entry key="dataSourceOne" value-ref="dataSourceOne" />
61                 <entry key="dataSourceTwo" value-ref="dataSourceTwo" />
62             </map>
63         </property>
64     </bean>
65     
66     <!-- MyBatis在spring中Bean的配置,都是固定的 -->
67     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
68         <property name="dataSource" ref="dataSource" />
69         <property name="configLocation" value="classpath:mybatisConfig.xml" />
70     </bean>
71     <bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
72         <constructor-arg index="0" ref="sqlSessionFactory" />
73     </bean>
74 
75     <!-- 事务管理 -->
76     <!-- 事务注解驱动,标注@Transactional的类和方法将具有事务性 -->
77     <tx:annotation-driven transaction-manager="transactionManager" />
78 
79     <bean id="transactionManager"
80         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
81         <property name="dataSource" ref="dataSource"></property>
82     </bean>
83 </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();
	}
}

  

 

spring多数据源配置

标签:

原文地址:http://www.cnblogs.com/fengzhentian/p/4601886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!