一个项目多数据库配置

有时一个项目需要连接两个数据库,下面就是个人的实现步骤:

首先创建项目目录如下:

一个项目多数据库配置_第1张图片

web.xml


        xmlns="http://java.sun.com/xml/ns/javaee" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  tkbsTomlr
  
      
     
     
        webmvc  
        org.springframework.web.servlet.DispatcherServlet  
         
           
            contextConfigLocation    
            classpath:spring-mvc.xml  
       
   
        1    
   
 
     
        webmvc  
        /  
   
 
      
     
     
        org.springframework.web.context.ContextLoaderListener  
   
 
      
     
     
        contextConfigLocation  
        classpath:applicationContext.xml  
   
 
      
       
     
        encodingFilter  
        org.springframework.web.filter.CharacterEncodingFilter  
         
            encoding  
            UTF-8  
       
 
   
 
     
        encodingFilter  
        /*  
   
 

 

spring-mvc.xml

 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans.xsd   
       http://www.springframework.org/schema/context   
       http://www.springframework.org/schema/context/spring-context.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx.xsd  
          http://www.springframework.org/schema/mvc  
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
      
       
      
      
     
     
  
     
     
      
     
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
         
         
   
  
    

applicationContext.xml

 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd   
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd   
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
     
     
     
     
     
     
   
    
        
       
       
       
       
       
       
       
       
       
       
       
       
       
   
 
      
   
           
       
       
       
       
       
       
       
       
       
       
       
       
       
   
   
   
   
       
           
               
               
               
           

       

       
       
   

 
 
 
   
   
       
       
       
   

 
   
   
       
       
   

 
 
   
   
       
   

 
   
   

   
     
   
 
 

数据库主要配置

一个项目多数据库配置_第2张图片

 

一个项目多数据库配置_第3张图片

一个项目多数据库配置_第4张图片

创建DynamicDataSource类

一个项目多数据库配置_第5张图片

创建CustomerContextHolder类,利用ThreadLocal解决线程安全问题

一个项目多数据库配置_第6张图片

最后就是调用使用

在DAOImpl中切换数据源CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_MLR);

 

 

你可能感兴趣的:(Spring,线程and进程,Mysql)