javaweb项目配置多数据库

项目结构:

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

配置文件:applicationContext.xml

  
  
    
      
      
     
    
      
    
		
		
		
		
		
		
		
		
		
		
		
	  
  	
  	
        
    
    
        
    
  	
  	
        
            
                
                
            
        
        
    
  
      
      
          
      
	
	
  
       
          
         
      
      
          
    

  
jdbc.properties:

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:实例名
jdbc.username=用户名
jdbc.password=密码
jdbc.url1=jdbc:oracle:thin:@192.168.42.147:1521:第二个数据库实例名
#也可配置其他数据库

c3p0.initialPoolSize=10
c3p0.maxPoolSize=10
c3p0.minPoolSize=10
c3p0.maxStatements=10
常量类:

package com.cn.cncc.dynamicDS;

/**
 * 常量类
 * @author hjr
 *
 */
public class DataSourceConst {
	/*
	 * 操作本地数据库
	 */
    public static final String LOCAL = "local";
    /*
     * 操作BMC远端数据库
     */
    public static final String CNCC = "cncc";
}
动态数据源:

package com.cn.cncc.dynamicDS;

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

/**
 * 动态数据源
 * @author hjr
 *
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        // TODO Auto-generated method stub
        return DataSourceContextHolder.getDataSourceType();
    }

}
数据源动态切换方法类:

package com.cn.cncc.dynamicDS;

/**
 * 数据源切换方法类
 * @author hjr
 *
 */
public class DataSourceContextHolder {

    private static final ThreadLocal contextHolder = new ThreadLocal();//线程本地环境
    
    //设置数据源类型
    public static void  setDataSourceType(String dataSourceType)
    {
        contextHolder.set(dataSourceType);
    }
    
    //获取数据源类型
    public static String getDataSourceType()
    {
        return contextHolder.get();
    }
    
    //清除数据源类型
    public static void clearDataSourceType()
    {
        contextHolder.remove();
    }
}
测试类:
package test;

import java.io.BufferedReader;
import java.math.BigDecimal;
import java.sql.Clob;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.cncc.dynamicDS.DataSourceConst;
import com.cn.cncc.dynamicDS.DataSourceContextHolder;
import com.cn.cncc.service.CpuService;



public class dyanmicDataSourceTest {
	
    
	Logger log = Logger.getLogger(dyanmicDataSourceTest.class);

    private CpuService cpuServiceImpl;

	@Before
	public void before(){
		
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-servlet.xml","classpath:applicationContext.xml"});

		cpuServiceImpl = (CpuService)context.getBean("cpuServiceImpl");

	}
	
	@Test
	public void cpuTest(){
		String dataSourceType = DataSourceContextHolder.getDataSourceType();//当前数据库
		System.out.println(dataSourceType);
		List findcpu = cpuServiceImpl.findCpuTop5();
		if(null != findcpu && findcpu.size()>0){
			try {
				DataSourceContextHolder.setDataSourceType(DataSourceConst.LOCAL);//切换数据库
				int insertcpu = cpuServiceImpl.insertCpuTop5(findcpu);//在另一个数据库查询
				String dataSourceType2 = DataSourceContextHolder.getDataSourceType();
				System.out.println(dataSourceType2);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
}
 
  




你可能感兴趣的:(java,数据库,java,web,链接双数据库,spring框架,数据库)