spring mvc 配置多数据源

程序是使用的是spring JdbcTemplate架构,简要记录下配置多数据源的方法:

        

1,设置数据源:

        在application.properties文件中配置多个数据源,示例中我配置了2个数据源,如下:

A、oracle

jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@1localhost:1521:dev
jdbc.username=ling
jdbc.password=229

B、Mysql

#mysql
jdbc-mysql.driver=com.mysql.jdbc.Driver
jdbc-mysql.url=jdbc:mysql://localhost:3306/test
jdbc-mysql.username=root
jdbc-mysql.password=229 

2、配置applicationContext-dataSource.xml

        在applicationContext-dataSource.xml文件中获取数据源,程序使用的是jdbcTemplate架构,如下:



    
    
    
    
    

    
    
    
    
    




   
   
   
   

   
   
   
   




   
      
         
         
      
   
   
   

 
 

       
      
   

 3,动态数据源类

        从步骤2中,可以看到使用了DynamicDataSource这个类,这是一个动态数据源类

A、DynamicDataSource

public class DynamicDataSource extends AbstractRoutingDataSource{ 
	@Override
	protected Object determineCurrentLookupKey() { 		    
        return CustomerContextHolder.getCustomerType();
	} 
}

B、CustomerContextHolder

public class CustomerContextHolder {
	public static final String DATA_SOURCE_ORACLE = "dataSource";// oracle 数据源 和  配置的 key 一样
	public static final String DATA_SOURCE_MYSQL = "mysqlDataSource";// mysql数据源和配置的key一样 	private static final ThreadLocal contextHolder = new ThreadLocal();
	public static void setCustomerType(String customerType) {
		contextHolder.set(customerType);
	}

	public static String getCustomerType() {
		String dataSource = contextHolder.get();
		if (StringUtils.isEmpty(dataSource)) {
			return DATA_SOURCE_MYSQL;
		} else {
			return dataSource;
		}
	}

	public static void clearCustomerType() {
		contextHolder.remove();
	}

}

4,切换数据源

        建立service 切换数据源 最好在服务层切换

A、NodeMysqlService

@Service("nodeMysqlService") 
public class NodeMysqlService {
	   
	public String getMysqlCommandSeries() throws ApplicationException {
		NodeMysqlDao nodeMysqlDao = (NodeMysqlDao) ContextLoader.getCurrentWebApplicationContext().getBean("nodeMysqlDao");
		
		String commonSerials = "";
		try {
			CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_MYSQL); // 切换到mysql数据源

			commonSerials = nodeMysqlDao.getMysqlCommandSeries();
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException(e.getMessage());
		}finally{
			CustomerContextHolder.clearCustomerType(); 
// 切换到默认数据源
		}
		return commonSerials;
	}

}

B、NodeMysqlDao:

@Component
public class NodeMysqlDao {

	private  String MYSQL_GET_SEQUENCE = "select branch_id as id fron branch";
	@Autowired
	private JdbcTemplate jdbcTemplate;


	public String getMysqlCommandSeries() throws ApplicationException, SystemException {

		String sequence = "";
		Map sequenceMap = new HashMap();

		try
		{
			sequenceMap = jdbcTemplate.queryForMap(MYSQL_GET_SEQUENCE);
			sequence = Long.toString((Long)sequenceMap.get("id")) ;

		} catch (Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}

		return sequence;
	}

}

其它业务信息就自己建立。

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