springMVC mybatis 多数据源配置

1.ssm(spring+springMVC+mybatis)环境搭建(略)

2.工程目录及多数据源涉及的文件

springMVC mybatis 多数据源配置_第1张图片

1).DataSourceContextHolder.java

package com.zjgt.datasource;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

public class DataSourceContextHolder {
	private static final ThreadLocal contextHolder =new ThreadLocal<>();
	 public static void setDbType(String dbType){
		 contextHolder.set(dbType);
	 }
	 
	 public static String getDbType(){
		 return ((String)contextHolder.get());
	 }
	 public static void clearDbType(){
		 contextHolder.remove();
	 }

}
2).DynamicDataSource.java

package com.zjgt.datasource;

import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

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

public class DynamicDataSource extends AbstractRoutingDataSource {


	@Override
	protected Object determineCurrentLookupKey() {
		return DataSourceContextHolder.getDbType();
	}

}
3).jdbc.properties

db1.driverClassName=com.mysql.jdbc.Driver
db1.url=jdbc:mysql://localhost:3306/zjzb_qc?useUnicode=true&characterEncoding=UTF-8
db1.username=root
db1.password=123456

db2.driverClassName=com.mysql.jdbc.Driver
db2.url=jdbc:mysql://localhost:3306/zjzb_qc2?useUnicode=true&characterEncoding=UTF-8
db2.username=root
db2.password=123456

db3.driverClassName=com.mysql.jdbc.Driver
db3.url=jdbc:mysql://localhost:3306/zjzb_qc3?useUnicode=true&characterEncoding=UTF-8
db3.username=root
db3.password=123456
4).spring-mybatis.xml



	
	
	
	
 	
        
            
                classpath:jdbc.properties
                classpath:redis.properties
            
        
    


 
    
     
            
            
            
            
     
     
            
            
            
            
     
     
            
            
            
            
     
   
   
   
   	
		   
                  
                        
                        
                        
                  
            
                  
	
 

	
	
		
		
		
	

	
	
		
		
	

	
	
		
	

	
	

	
	
		
		
			
			
			
			
			
			
			
			
			
			
			
			
			
		
	

	
	
		
		
	

  
3.在web.xml引用spring-mybatis.xml即可

4.在controller里面配置数据源即可(由于service层配置了事务。一旦调用进入到事务内部,再切换数据源则无效。故放在controller里面)

代码示例如下:

@ResponseBody
	@RequestMapping(value = "_list", produces = "text/html;charset=UTF-8")
	public String userLists(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {

		//切换数据源到zjzb_qc2
		DataSourceContextHolder.setDbType("zjzb_qc2");
		String userName = request.getParameter("userName");
		String departmentName = request.getParameter("departmentName");
		String pageSize = request.getParameter("pageSize");
		String pageCurrent = request.getParameter("pageCurrent");
		int size = Integer.parseInt(pageSize);
		int currentPage = Integer.parseInt(pageCurrent);
		int start = size * (currentPage - 1);
		int totalSize = userService.getUserListSize(userName, departmentName);
		ArrayList userList = userService.getUserList(userName, departmentName, start, size);
		if (userList.size() > 0) {
			return ToolUtil.listToJson(userList, size, currentPage, totalSize, "yyyy-MM-dd");
		}
		return null;
	}


你可能感兴趣的:(mysql,spring,springMVC,mybatis)