首页 > 代码库 > spring多数据源配置

spring多数据源配置

项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源。绑定给sessionFactory,在dao层代码中再指定sessionFactory来进行数据库操作。

技术分享

正如上图所示,每一块都是指定绑死的,如果是多个数据源,也只能是下图中那种方式。

技术分享

可看出在Dao层代码中写死了两个SessionFactory,这样日后如果再多一个数据源,还要改代码添加一个SessionFactory,显然这并不符合开闭原则。

那么正确的做法应该是下载 

技术分享

代码:下载 

1. applicationContext.xml

[html] view plain copy

 

  1. xml version="1.0" encoding="UTF-8"?>  

  2. << span="">beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  

  4.     xmlns:cache="http://www.springframework.org/schema/cache"  

  5.     xmlns:context="http://www.springframework.org/schema/context"  

  6.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  

  7.     xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"  

  8.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"  

  9.     xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  

  10.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  

  11.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    

  12.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    

  13.     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    

  14.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd    

  15.     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    

  16.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd    

  17.     http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd    

  18.     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd    

  19.     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd    

  20.     http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd    

  21.     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd    

  22.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    

  23.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">  

  24.   

  25.     << span="">context:annotation-config />  

  26.   

  27.     << span="">context:component-scan base-package="com">context:component-scan>  

  28.   

  29.     << span="">bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

  30.         << span="">property name="locations">  

  31.             << span="">list>  

  32.                 << span="">value>classpath:com/resource/config.propertiesvalue>  

  33.             list>  

  34.         property>  

  35.     bean>  

  36.   

  37.     << span="">bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource"  

  38.         destroy-method="close">  

  39.         << span="">property name="driverClass" value="${dbOne.jdbc.driverClass}" />  

  40.         << span="">property name="jdbcUrl" value="${dbOne.jdbc.url}" />  

2. DynamicDataSource.class下载 

[java] view plain copy

 

  1. package com.core;  

  2.   

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

  4.   

  5. public class DynamicDataSource extends AbstractRoutingDataSource{  

  6.   

  7.     @Override  

  8.     protected Object determineCurrentLookupKey() {  

  9.         return DatabaseContextHolder.getCustomerType();   

  10.     }  

  11.   

  12. }  

3. DatabaseContextHolder.class下载 

[java] view plain copy

 

  1. package com.core;  

  2.   

  3. public class DatabaseContextHolder {  

  4.   

  5.     private static final ThreadLocal contextHolder = new ThreadLocal();  

  6.   

  7.     public static void setCustomerType(String customerType) {  

  8.         contextHolder.set(customerType);  

  9.     }  

  10.   

  11.     public static String getCustomerType() {  

  12.         return contextHolder.get();  

  13.     }  

  14.   

  15.     public static void clearCustomerType() {  

  16.         contextHolder.remove();  

  17.     }  

  18. }  

4. DataSourceInterceptor.class

[java] view plain copy

 

  1. package com.core;  

  2.   

  3. import org.aspectj.lang.JoinPoint;  

  4. import org.springframework.stereotype.Component;  

  5.   

  6. @Component  

  7. public class DataSourceInterceptor {  

  8.   

  9.     public void setdataSourceOne(JoinPoint jp) {  

  10.         DatabaseContextHolder.setCustomerType("dataSourceOne");  

  11.     }  

  12.       

  13.     public void setdataSourceTwo(JoinPoint jp) {  

  14.         DatabaseContextHolder.setCustomerType("dataSourceTwo");  

  15.     }  

  16. }  

5. po实体类下载 

[java] view plain copy

 

  1. package com.po;  

  2.   

  3. import javax.persistence.Column;  

  4. import javax.persistence.Entity;  

  5. import javax.persistence.Id;  

  6. import javax.persistence.Table;  

  7.   

  8. @Entity  

  9. @Table(name = "BTSF_BRAND", schema = "hotel")  

  10. public class Brand {  

  11.   

  12.     private String id;  

  13.     private String names;  

  14.     private String url;  

  15.   

  16.     @Id  

  17.     @Column(name = "ID", unique = true, nullable = false, length = 10)  

  18.     public String getId() {  

  19.         return this.id;  

  20.     }  

  21.   

  22.     public void setId(String id) {  

  23.         this.id = id;  

  24.     }  

  25.   

  26.     @Column(name = "NAMES", nullable = false, length = 50)  

  27.     public String getNames() {  

  28.         return this.names;  

  29.     }  

  30.   

  31.     public void setNames(String names) {  

  32.         this.names = names;  

  33.     }  

  34.   

  35.     @Column(name = "URL", length = 200)  

  36.     public String getUrl() {  

  37.         return this.url;  

  38.     }  

  39.   

  40.     public void setUrl(String url) {  

  41.         this.url = url;  

  42.     }  

  43. }  

[java] view plain copy

 

  1. package com.po;  

  2.   

  3. import javax.persistence.Column;  

  4. import javax.persistence.Entity;  

  5. import javax.persistence.Id;  

  6. import javax.persistence.Table;  

  7.   

  8. @Entity  

  9. @Table(name = "CITY", schema = "car")  

  10. public class City {  

  11.   

  12.     private Integer id;  

  13.       

  14.     private String name;  

  15.   

  16.     @Id  

  17.     @Column(name = "ID", unique = true, nullable = false)  

  18.     public Integer getId() {  

  19.         return id;  

  20.     }  

  21.   

  22.     public void setId(Integer id) {  

  23.         this.id = id;  

  24.     }  

  25.   

  26.     @Column(name = "NAMES", nullable = false, length = 50)  

  27.     public String getName() {  

  28.         return name;  

  29.     }  

  30.   

  31.     public void setName(String name) {  

  32.         this.name = name;  

  33.     }  

  34. }  

6. BrandDaoImpl.class下载 

spring多数据源配置