ssm 多数据源 配置

1 xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		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-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-3.1.xsd">


	<!-- <context:component-scan base-package="org.cdsoft.*"></context:component-scan> -->
	<context:component-scan base-package="sys.cdsoft.*"></context:component-scan>
	
	
	
	<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
			<property name="locations">
				<array>
					<value>classpath*:db.properties</value>
				</array>
			</property>	
	</bean>
	
	
	 	 
	<!-- 配置数据库信息 -->
	<bean id="dataSourceMysql" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		
		<property name="url" value="${url}"></property>
		
		<property name="username" value="${username}"></property>
		
		<property name="password" value="${password}"></property>
		
		<property name="maxActive" value="10"></property>
		
		<property name="maxIdle" value="6"></property>
		<!-- 防止mysql等待时间超时报错   -->
		<property name="testOnBorrow" value="false"></property>
		
		<property name="testOnReturn" value="false"></property>
		
		<property name="testWhileIdle" value="true"></property>
		
		<property name="timeBetweenEvictionRunsMillis" value="100000"></property>
		
		<property name="validationQuery" value="select 1"></property>
	</bean>
	
	<!-- 配置数据库信息2 -->
	<bean id="dataSourceMysql2" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		
		<property name="url" value="${url2}"></property>
		
		<property name="username" value="${username2}"></property>
		
		<property name="password" value="${password2}"></property>
		
		<property name="maxActive" value="10"></property>
		
		<property name="maxIdle" value="6"></property>
		<!-- 防止mysql等待时间超时报错   -->
		<property name="testOnBorrow" value="false"></property>
		
		<property name="testOnReturn" value="false"></property>
		
		<property name="testWhileIdle" value="true"></property>
		
		<property name="timeBetweenEvictionRunsMillis" value="100000"></property>
		
		<property name="validationQuery" value="select 1"></property>
	</bean>
	
	<bean id="dataSource" class="sys.cdsoft.util.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                
                <entry key="dataSourceMysql" value-ref="dataSourceMysql"></entry>
                <entry key="dataSourceMysql2" value-ref="dataSourceMysql2"></entry>
            </map>
        </property>
       
        <property name="defaultTargetDataSource" ref="dataSourceMysql" />
    </bean> 
 
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  		<property name="basePackage" value="sys.cdsoft.mapper" />
  		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		 
		<property name="mapperLocations" value="classpath*:sys/cdsoft/*/*.xml"></property>
		
		
		<property name="configLocation" value="classpath:mybatis.xml"></property>
		
	</bean> 


	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>
    
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
	    <property name="messageConverters">  
	        <list>  
	               <ref bean="mappingJacksonHttpMessageConverter" />  
	        </list>  
	    </property>  
	</bean>  
	

	
</beans>

2. Java 继承 AbstractRoutingDataSource类

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

public class DynamicDataSource extends AbstractRoutingDataSource{

	@Override
	protected Object determineCurrentLookupKey() {
		return DynamicDataSourceHolder.getDataSource();
	}

}

设定 数据源工具类

public class DynamicDataSourceHolder {
	private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal();

	public static String getDataSource() {
		return (String) THREAD_DATA_SOURCE.get();
	}

	public static void setDataSource(String dataSource) {
		THREAD_DATA_SOURCE.set(dataSource);
	}

	public static void clearDataSource() {
		THREAD_DATA_SOURCE.remove();
	}

}

代码中具体实战

	//查询符合条件的客户信息
	@RequestMapping("selectall")
	public JSONObject selectAll(Bankdetails bankdetails) {
		//此处设定连接的数据源 是哪个
		DynamicDataSourceHolder.setDataSource("dataSourceMysql2");
		System.out.println("前端传输数据为:"+gson.toJson(bankdetails));
		CustomUtil util = new CustomUtil();
		JSONObject jsonObject=new JSONObject();
		if (StringUtil.isNotEmpty(bankdetails.getCreate_time())) {
			String times[]=TimeUtil.reTimes(bankdetails.getCreate_time(), " - ");
			bankdetails.setStart_time(times[0]);
			bankdetails.setEnd_time(times[1]);
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值