多数据源的配置和切换

[color=green]
项目中用到过多数据源之间的切换,从网络上搜寻到的相关资料结合自己的实践,把它记录下来,以备需要时查询。
一、数据源的配置。
代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 数据源定义-->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/tenpay_db?user=tenpay&password=cft2008</value>
</property>
<property name="username">
<value>tenpay</value>
</property>
<property name="password">
<value>cft2008</value>
</property>
</bean>

<bean id="fcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/c2c_db?user=tenpay&password=cft2008</value>
</property>
<property name="username">
<value>tenpay</value>
</property>
<property name="password">
<value>cft2008</value>
</property>
</bean>

<bean id="dataSource" class="com.ben.util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="com.ben.util.CustomerType">
<entry key="fcds" value-ref="fcDataSource"/>
<entry key="myds" value-ref="myDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="fcDataSource"/>
</bean>

</beans>

数据源枚举类:
代码:

/**
* <pre>
* Title: CustomerType.java
* Project: tenpay_Admin
* Type: com.ben.util.CustomerType
* Author: ben.liu
* Create: 2008-11-25 下午05:12:49
* Copyright: Copyright (c) 2008
* Company:
* <pre>
*/
package com.ben.util;

/**
* <pre>
* 数据源枚举类
* </pre>
* @author ben.liu
* @version 1.0, 2008-11-25
*/
public enum CustomerType
{
fcds,
myds
}

动态数据源:
代码:

/**
* <pre>
* Title: DynamicDataSource.java
* Project: tenpay_Admin
* Type: com.ben.util.DynamicDataSource
* Author: ben.liu
* Create: 2008-11-25 下午02:26:46
* Copyright: Copyright (c) 2008
* Company:
* <pre>
*/
package com.ben.util;

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


/**
* <pre>
* 动态数据源
* </pre>
* @author ben.liu
* @version 1.0, 2008-11-25
*/
public class DynamicDataSource extends AbstractRoutingDataSource
{

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

}

数据源切换类:
代码:

/**
* <pre>
* Title: CustomerContextHolder.java
* Project: tenpay_Admin
* Type: com.ben.util.CustomerContextHolder
* Author: ben.liu
* Create: 2008-11-25 下午02:22:52
* Copyright: Copyright (c) 2008
* Company:
* <pre>
*/
package com.ben.util;

/**
* <pre>
* 数据源切换类
* </pre>
* @author ben.liu
* @version 1.0, 2008-11-25
*/
public class CustomerContextHolder
{
private static final ThreadLocal<CustomerType> contextHolder = new ThreadLocal<CustomerType>();

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

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

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

当需要数据库操作时,需要在操作前增加这段代码即可:

CustomerContextHolder.setCustomerType(CustomerType.myds);

[/color]
展开阅读全文

没有更多推荐了,返回首页