1, first of all required Writing a static control key categories:
Java code
package cn.com.xinli.ccp.dynamicds;
public class DataSourceMap (
public static final String Admin = "Admin";
public static final String Yxh = "Yxh";
)
package cn.com.xinli.ccp.dynamicds; public class DataSourceMap (public static final String Admin = "Admin"; public static final String Yxh = "Yxh";)
This category in which they can use when access to the data source as a sign of use.
Second, the establishment of a context to obtain and set up categories:
Java code
package cn.com.xinli.ccp.dynamicds;
public class CustomerContextHolder (
private static final ThreadLocal contextHolder =
new ThreadLocal ();
public static void setCustomerType (String customerType) (
contextHolder.set (customerType);
)
public static String getCustomerType () (
return (String) contextHolder.get ();
)
public static void clearCustomerType () (
contextHolder.remove ();
)
)
package cn.com.xinli.ccp.dynamicds; public class CustomerContextHolder (private static final ThreadLocal contextHolder = new ThreadLocal (); public static void setCustomerType (String customerType) (contextHolder.set (customerType);) public static String getCustomerType () ( return (String) contextHolder.get ();) public static void clearCustomerType () (contextHolder.remove ();))
This is mainly responsible for setting the context of environment and context.
Three, set up the dynamic data source type, this class must inherit AbstractRoutingDataSource:
Java code
package cn.com.xinli.ccp.dynamicds;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource (
protected Object determineCurrentLookupKey () (
/ / TODO Auto-generated method stub
return CustomerContextHolder.getCustomerType ();
)
)
package cn.com.xinli.ccp.dynamicds; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DynamicDataSource extends AbstractRoutingDataSource (protected Object determineCurrentLookupKey () (/ / TODO Auto-generated method stub return CustomerContextHolder.getCustomerType () ;))
This class determineCurrentLookupKey achieve a method that returns an Object, is generally return string can also be enumerated type. The method is directly used CustomerContextHolder.getCustomerType () method to obtain the context of the environment and directly back.
Four, the preparation of the spring configuration file to configure the data source
Java code
<bean
>
<property name="driverClassName">
<value> COM.ibm.db2.jdbc.net.DB2Driver </ value>
</ property>
<property name="url">
<value> jdbc: db2: 127.0.0.1: TEST </ value>
</ property>
</ bean>
<bean parent="parentDataSource">
<property name="username" value="admin"/>
<property name="password" value="master997mb"/>
</ bean>
<bean parent="parentDataSource">
<property name="username" value="yxh"/>
<property name="password" value="yxh"/>
</ bean>
<bean class = "org.springframework.jdbc.datasource.DriverManagerDataSource" gt; lt; property name = "driverClassName" gt; lt; valuegt; COM.ibm.db2.jdbc.net.DB2Driverlt; / valuegt; lt; / propertygt ; lt; property name = "url" gt; lt; valuegt; jdbc: db2: 127.0.0.1: TESTlt; / valuegt; lt; / propertygt; lt; / beangt; lt; bean parent = "parentDataSource" gt; lt; property name = "username" value = "admin" / gt; lt; property name = "password" value = "master997mb" / gt; lt; / beangt; lt; bean parent = "parentDataSource" gt; lt; property name = "username" value = "yxh" / gt; lt; property name = "password" value = "yxh" / gt; lt; / beangt;
In this configuration can be seen first of all, there is a parentDataSource, the main data source to configure some common information, projects are DB2 database link; adminDataSource and yxhDataSource are required according to different configuration of personalized information, but must add parent property, value of parentDataSource. Configured so that two data source information. Of course, if the link many data sources are two different types of databases, then you can parentDataSource not to the direct allocation of two different data sources link it.
Friday, the preparation of many spring configuration file to configure the data source mapping relations
Java code
<bean>
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="Yxh" value-ref="yxhDataSource"/>
</ map>
</ property>
<property name="defaultTargetDataSource" ref="adminDataSource"/>
</ bean>
<beangt; lt; property name = "targetDataSources" gt; lt; map key-type = "java.lang.String" gt; lt; entry key = "Yxh" value-ref = "yxhDataSource" / gt; lt; / mapgt; lt; / propertygt; lt; property name = "defaultTargetDataSource" ref = "adminDataSource" / gt; lt; / beangt;
In this configuration the first property to configure the target data source property, <map key-type = "java.lang.String" gt; the key-type key must be controlled and static type of the value of DataSourceMap the same type; lt ; entry key = "Yxh" value-ref = "yxhDataSource" / gt; Medium key value must be the control key and static values in the same category, if there is multiple values, can be configured in more than lt; entrygt; tag. The second property of the property to configure the default data source.
6, configuration hibernate.
Hibernate configuration and ordinary hibernate, spring combined with the configuration of the same
Java code
<bean
>
<! - To override, use the "SpringDatasourceConfig" snippet in your project ->
<property name="dataSource">
<ref local="dataSource" />
</ property>
<property name="mappingResources">
<list>
<value>
cn / com / xinli / ccp / entity / User.hbm.xml
</ value>
<value>
cn / com / xinli / ccp / entity / Test.hbm.xml
</ value>
</ list>
</ property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.DB2Dialect
</ prop>
<prop key="hibernate.show_sql"> true </ prop>
<prop key="hibernate.use_outer_join"> true </ prop>
<prop key="hibernate.jdbc.batch_size"> 50 </ prop>
<prop key="hibernate.jdbc.fetch_size"> 5 </ prop>
<prop key="hibernate.connection.pool_size"> 2 </ prop>
<prop key="hibernate.connection.autocommit"> false </ prop>
<prop key="hibernate.cache.use_query_cache"> false </ prop>
<prop key="hibernate.max_fetch_depth"> 1 </ prop>
<prop key="hibernate.bytecode.use_reflection_optimizer"> true </ prop>
</ props>
</ property>
</ bean>
<bean>
<property name="sessionFactory">
<ref local="sessionFactory" />
</ property>
</ bean>
<bean class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" gt; lt;! - to override, use the "SpringDatasourceConfig" snippet in your project - gt; lt; property name = "dataSource" gt; lt; ref local = "dataSource" / gt; lt; / propertygt; lt; property name = "mappingResources" gt; lt; listgt; lt; valuegt; cn / com / xinli / ccp / entity / User.hbm.xml lt; / valuegt; lt; valuegt; cn / com / xinli / ccp / entity / Test.hbm.xml lt; / valuegt; lt; / listgt; lt; / propertygt; lt; property name = "hibernateProperties" gt; lt; propsgt; lt; prop key = "hibernate.dialect" gt; org.hibernate.dialect.DB2Dialect lt; / propgt; lt; prop key = "hibernate.show_sql" gt; truelt; / propgt; lt; prop key = "hibernate.use_outer_join "gt; truelt; / propgt; lt; prop key =" hibernate.jdbc.batch_size "gt; 50lt; / propgt; lt; prop key =" hibernate.jdbc.fetch_size "gt; 5lt; / propgt; lt; prop key = "hibernate.connection.pool_size" gt; 2lt; / propgt; lt; prop key = "hibernate.connection.autocommit" gt; falselt; / propgt; lt; prop key = "hibernate.cache.use_query_cache" gt; falselt; / propgt; lt; prop key = "hibernate.max_fetch_depth" gt; 1lt; / propgt; lt; prop key = "hibernate.bytecode.use_reflection_optimizer" gt; truelt; / propgt; lt; / propsgt; lt; / propertygt; lt ; / beangt; lt; beangt; lt; property name = "sessionFactory" gt; lt; ref local = "sessionFactory" / gt; lt; / propertygt; lt; / beangt;
About dao code omitted here.
Seven, configure the end, you can use the.
Java code
public class DaoTest extends TestCase (
public void testSave () throws Exception (
CustomerContextHolder.setCustomerType (DataSourceMap.Admin); / / Set the data source
/ / hibernate create entities
Test test = new Test ();
test.setTest ( "22222222");
mydao.save (test); / / use dao preservation entities
CustomerContextHolder.setCustomerType (DataSourceMap.Yxh); / / set to another data source
mydao.save (test); / / use dao preservation entity to another library
)
)
public class DaoTest extends TestCase (public void testSave () throws Exception (CustomerContextHolder.setCustomerType (DataSourceMap.Admin); / / Set the data source / / hibernate create entities Test test = new Test (); test.setTest ( "22222222"); mydao.save (test); / / use dao preservation entities CustomerContextHolder.setCustomerType (DataSourceMap.Yxh); / / set to another data source mydao.save (test); / / use dao preservation entity to another library)) in the project staff for the coding of many data sources can be made transparent switch, operation of the same dao, can access a different database.







