java单数据源改多数据源配置(hibernate)

xml配置



	
	 
	 	
	      
	          
	          
	          
	          
	       	
			
			
			
			
			
			
			
			
			
			
			
	      
	    
	  	
	      
	          
	        	
	              
	                  
					
	              
	          
	        
	          
	          
	      
	    
	  	
	  		
			  
			
			
				
			
			
				
					${hibernate.dialect}
					${hibernate.show_sql}
					${hibernate.format_sql}
					org.springframework.orm.hibernate4.SpringSessionContext
					${hibernate.hbm2ddl.auto}
					true
					true
					org.hibernate.cache.ehcache.EhCacheRegionFactory
					com/cl/frame/config/cache/ehcache-hibernate-local.xml
				
			
			
		
	  
		
		
			
		
		
			
		
	


jdbc配置文件

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@ip\:1521\:orcl
jdbc.username=out_work
jdbc.password=out_work
dbTwo.jdbc.driverClass=oracle.jdbc.driver.OracleDriver
dbTwo.jdbc.url=jdbc\:oracle\:thin\:@ip\:1521\:orcl
dbTwo.jdbc.user=oa
dbTwo.jdbc.password=oa
dbTwo.jdbc.initialPoolSize= 4
dbTwo.jdbc.minPoolSize =1
dbTwo.jdbc.maxPoolSize=4
#hibernate.show_sql=true

java文件

package com.cl.frame.clfbs.moudels.core;

public class DatabaseContextHolder {  
	  
    private static final ThreadLocal contextHolder = new ThreadLocal();  
  
    public static void setCustomerType(String customerType) {  
        contextHolder.set(customerType);  
    }  
  
    public static String getCustomerType() {  
        return contextHolder.get();  
    }  
  
    public static void clearCustomerType() {  
        contextHolder.remove();  
    }  
}  

package com.cl.frame.clfbs.moudels.core;

import org.aspectj.lang.JoinPoint;


public class DataSourceInterceptor {  
  
    public void setdataSourceOne(JoinPoint jp) {  
    	System.out.println("AreaOnSeriveImp==="+"DataSourceInterceptor");
    	//清除原数据源
    	DatabaseContextHolder.clearCustomerType();
    	//设置新数据源
        DatabaseContextHolder.setCustomerType("dataSource");  
    }  
      
    public void setdataSourceTwo(JoinPoint jp) {  
    	System.out.println("AreaTwoServiceImp==="+"DataSourceInterceptor");
    	DatabaseContextHolder.clearCustomerType();
        DatabaseContextHolder.setCustomerType("dataSourceTwo");  
    }  
}  
package com.cl.frame.clfbs.moudels.core;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class DataSourceAspect implements MethodBeforeAdvice,AfterReturningAdvice   
{  
  
    @Override  
    public void afterReturning(Object returnValue, Method method,  
            Object[] args, Object target) throws Throwable {  
        // TODO Auto-generated method stub  
        DatabaseContextHolder.clearCustomerType();  
    }

	@Override
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		
	}  
  
    
}  
package com.cl.frame.clfbs.moudels.core;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
 * 每次请求数据库都会访问该类,调用下面方法
 * @author lovem
 *
 */
public class DynamicDataSource extends AbstractRoutingDataSource{

	//
	protected Object determineCurrentLookupKey() {
		/*System.out.println(DatabaseContextHolder.getCustomerType()+"===DynamicDataSource");
		System.out.println("===========================");*/
		return DatabaseContextHolder.getCustomerType();
	}

}

注意:数据源切换只能在业务层或者控制层,不能再持久层做数据源的切换

你可能感兴趣的:(hibernate,java,数据源)