SpringMVC配置双数据源,一个java项目同时连接两个数据库

数据源在配置文件中的配置

[java]  view plain  copy

 print?


  1. “code” class=“java”>“1.0” encoding=“UTF-8”?>  
  2. ”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.       
  26.   
  27.     package=“com”>  
  28.   
  29.     class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>  
  30.         ”locations”>  
  31.               
  32.                 classpath:com/resource/config.properties  
  33.               
  34.           
  35.       
  36.   
  37.     ”dataSourceOne” class=“com.mchange.v2.c3p0.ComboPooledDataSource”  
  38.         destroy-method=”close”>  
  39.         ”driverClass” value=“${dbOne.jdbc.driverClass}” />  
  40.         ”jdbcUrl” value=“${dbOne.jdbc.url}” />  
  41.         ”user” value=“${dbOne.jdbc.user}” />  
  42.         ”password” value=“${dbOne.jdbc.password}” />  
  43.         ”initialPoolSize” value=“${dbOne.jdbc.initialPoolSize}” />  
  44.         ”minPoolSize” value=“${dbOne.jdbc.minPoolSize}” />  
  45.         ”maxPoolSize” value=“${dbOne.jdbc.maxPoolSize}” />  
  46.       
  47.   
  48.     ”dataSourceTwo” class=“com.mchange.v2.c3p0.ComboPooledDataSource”  
  49.         destroy-method=”close”>  
  50.         ”driverClass” value=“${dbTwo.jdbc.driverClass}” />  
  51.         ”jdbcUrl” value=“${dbTwo.jdbc.url}” />  
  52.         ”user” value=“${dbTwo.jdbc.user}” />  
  53.         ”password” value=“${dbTwo.jdbc.password}” />  
  54.         ”initialPoolSize” value=“${dbTwo.jdbc.initialPoolSize}” />  
  55.         ”minPoolSize” value=“${dbTwo.jdbc.minPoolSize}” />  
  56.         ”maxPoolSize” value=“${dbTwo.jdbc.maxPoolSize}” />  
  57.       
  58.   
  59.     ”dynamicDataSource” class=“com.core.DynamicDataSource”>  
  60.         ”targetDataSources”>  
  61.             ”java.lang.String”>  
  62.                 ”dataSourceOne” key=“dataSourceOne”>  
  63.                 ”dataSourceTwo” key=“dataSourceTwo”>  
  64.               
  65.           
  66.         ”defaultTargetDataSource” ref=“dataSourceOne”>  
  67.           
  68.       
  69.   
  70.     ”sessionFactory” class=“org.springframework.orm.hibernate4.LocalSessionFactoryBean”>  
  71.         ”dataSource” ref=“dynamicDataSource” />  
  72.         ”hibernateProperties”>  
  73.               
  74.                 ”hibernate.dialect”>org.hibernate.dialect.MySQLDialect  
  75.                 ”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext  
  76.                 ”hibernate.show_sql”>false  
  77.                 ”hibernate.format_sql”>true  
  78.                 ”hbm2ddl.auto”>create  
  79.               
  80.           
  81.         ”packagesToScan”>  
  82.               
  83.                 com.po  
  84.               
  85.           
  86.       
  87.   
  88.     ”transactionManager” class=“org.springframework.orm.hibernate4.HibernateTransactionManager”>  
  89.         ”sessionFactory” ref=“sessionFactory” />  
  90.       
  91.   
  92.       
  93.         ”transactionPointCut” expression=“execution(* com.dao..*.*(..))” />  
  94.         ”txAdvice” pointcut-ref=“transactionPointCut” />  
  95.       
  96.   
  97.     ”txAdvice” transaction-manager=“transactionManager”>  
  98.           
  99.             ”add*” propagation=“REQUIRED” />  
  100.             ”save*” propagation=“REQUIRED” />  
  101.             ”update*” propagation=“REQUIRED” />  
  102.             ”delete*” propagation=“REQUIRED” />  
  103.             ”*” read-only=“true” />  
  104.           
  105.       
  106.   
  107.       
  108.         ”dataSourceAspect” ref=“dataSourceInterceptor”>  
  109.             ”daoOne” expression=“execution(* com.dao.one.*.*(..))” />  
  110.             ”daoTwo” expression=“execution(* com.dao.two.*.*(..))” />  
  111.             ”daoOne” method=“setdataSourceOne” />  
  112.             ”daoTwo” method=“setdataSourceTwo” />  
  113.           
  114.       
  115.   






DynamicDataSource.class

[java]  view plain  copy
 print ?
  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. }  
 DatabaseContextHolder.class设置数据源的类

[java]  view plain  copy
 print ?
  1. package com.core;  
  2.   
  3. public class DatabaseContextHolder {  
  4.   
  5.     private static final ThreadLocal contextHolder = new ThreadLocal();  
  6. ”white-space:pre”>    //设置要使用的数据源  
  7.     public static void setCustomerType(String customerType) {  
  8.         contextHolder.set(customerType);  
  9.     }  
  10. ”white-space:pre”>    //获取数据源  
  11.     public static String getCustomerType() {  
  12.         return contextHolder.get();  
  13.     }  
  14. ”white-space:pre”>    //清除数据源,使用默认的数据源  
  15.     public static void clearCustomerType() {  
  16.         contextHolder.remove();  
  17.     }  
  18. }  
DataSourceInterceptor.class
[java]  view plain  copy
 print ?
  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. ”white-space:pre”>    //数据源1  
  9.     public static final String SOURCE_PLAN = font-family: Arial, Helvetica, sans-serif;“>dataSourceOnefont-family: Arial, Helvetica, sans-serif;“>”;  
  10.     //数据源2  
  11. ”code” class=“java”>“white-space:pre”>    public static final String SOURCE_FUND = font-family: Arial, Helvetica, sans-serif;“>dataSourceTwo;  
}

springMVC数据源

[html]  view plain  copy

 print?


  1. jdbc_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver  
  2. <pre name=“code” class=“java”>dataSourceOne<span style=“font-family: Arial, Helvetica, sans-serif;”>=jdbc:sqlserver://115.29.***.**;DatabaseName=DB_GuiHuaspan>  


jdbc_username=jdbc_password=
[java]  view plain  copy

 print?


  1. dataSourceTwo“font-family: Arial, Helvetica, sans-serif;”>=jdbc:sqlserver://115.29.***.*;DatabaseName=DB_Fund  


Spring MVC会默认有一个数据源,当需要更换数据源时,要在调用事务之前配置

[java]  view plain  copy

 print?


  1. DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);//更换数据源  

[java]  view plain  copy

 print?


  1. /** 
  2.  * @ClassName: DataSourceContextHolder 
  3.  * @Description: 数据库切换工具类 
  4.  * @author: wzx 
  5.  * @date: 2016-07-27 上午10:26:01 
  6.  */  
  7. public class DataSourceContextHolder {  
  8.     private static final ThreadLocal contextHolder = new ThreadLocal();  
  9.   
  10.     public static void setDbType(String dbType) {  
  11.         contextHolder.set(dbType);  
  12.     }  
  13.   
  14.     public static String getDbType() {  
  15.         return ((String) contextHolder.get());  
  16.     }  
  17.   
  18.     public static void clearDbType() {  
  19.         contextHolder.remove();  
  20.     }  
  21. }  

你可能感兴趣的:(spring,mvc,数据库,spring,数据库)