springMVC+myBatis多数据源

1.在同一服务器上

数据库名.表名



2.不在同一服务器上


spring配置文件spring-base.xml:

<!-- Druid数据源配置 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
		<property name="maxActive" value="${db.maxActive}" />
		<property name="initialSize" value="${db.initialSize}" />
		<property name="maxWait" value="${db.maxWait}" />
		<property name="minIdle" value="${db.minIdle}" />

		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxOpenPreparedStatements" value="20" />
	</bean>
	<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="url" value="${db2.url}" />
		<property name="username" value="${db2.username}" />
		<property name="password" value="${db2.password}" />
		<property name="maxActive" value="${db2.maxActive}" />
		<property name="initialSize" value="${db2.initialSize}" />
		<property name="maxWait" value="${db2.maxWait}" />
		<property name="minIdle" value="${db2.minIdle}" />

		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxOpenPreparedStatements" value="20" />
	</bean>
	
	<bean id="multipleDataSource" class="cn.com.zdyy.statistical.producttest.Common.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="dataSource"/>
        <property name="dataSource">
            <map>
                <entry key="dataSource" value-ref="dataSource"/>
                <entry key="dataSource2" value-ref="dataSource2"/>
            </map>
        </property>
    <span style="white-space:pre">	</span></bean>
<span style="white-space:pre">	</span><!-- MyBatis配置 -->
<span style="white-space:pre">	</span><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<span style="white-space:pre">		</span><property name="dataSource" ref="multipleDataSource" />
<span style="white-space:pre">		</span><property name="mapperLocations" value="classpath*:cn/com/...Dao/mapper/*.xml" />
<span style="white-space:pre">	</span></bean>


MultipleDataSource类

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

public class MultipleDataSource extends AbstractRoutingDataSource {

	private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();

	public static void setDataSourceKey(String dataSource) {
		dataSourceKey.set(dataSource);
	}

	@Override
	protected Object determineCurrentLookupKey() {
		return dataSourceKey.get();
	}

}


数据源1查询语句

import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
public interface dbMapper {
	@Select("select * from tools LIMIT 0, 10")
	List<Tools> getList();
}


数据源2查询语句

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
public interface dbMapper2 {
	@Select("select * from tools LIMIT 0, 10")
	List<Tools> getList();
}



测试Service

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
<span style="white-space:pre">	</span>@Autowired
<span style="white-space:pre">	</span>dbMapper dbMapper;
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>@Autowired
<span style="white-space:pre">	</span>dbMapper2 dbMapper2;

	/**
	 * 多数据源测试
	 */
	@Override
	public void testDb() {
		// TODO Auto-generated method stub

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-base.xml");

		dbMapper dbMapper = applicationContext.getBean(dbMapper.class);

		dbMapper2 dbMapper2 = applicationContext.getBean(dbMapper2.class);

		// 设置数据源为MySql,使用了AOP测试时请将下面这行注释
//		MultipleDataSource.setDataSourceKey("dataSource");
//		List<Tools> db = dbMapper.getList();
		// 设置数据源为SqlServer,使用AOP测试时请将下面这行注释
		MultipleDataSource.setDataSourceKey("dataSource2");
		List<Tools> db = dbMapper2.getList();

	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值