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();
}
}
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();
}
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();
}