PS:使用AbstractRoutingDataSource路由数据源实现动态数据库的调用
1、 连接哪个数据源的环境变量
package com.hysoft.common;
public class JdbcContextHolder {
private static final ThreadLocal<String> CONTEXTHOLDER = new ThreadLocal<String>();
public static void setJdbcType(String jdbcType) {
CONTEXTHOLDER.set(jdbcType);
}
/**
* 写读数据库
*/
public static void setWriteDataSource() {
setJdbcType("writeDataSource");
}
/**
* 设置读数据库
*/
public static void setReadyDataSource() {
clearJdbcType();
}
public static String getJdbcType() {
return (String) CONTEXTHOLDER.get();
}
public static void clearJdbcType() {
CONTEXTHOLDER.remove();
}
2、建立动态数据源类,这个类必须继承AbstractRoutingDataSource
package com.hysoft.common;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return JdbcContextHolder.getJdbcType();
}
}
3、配置数据库初始化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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 数据库连接 -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean> -->
<!-- ready only DataSource -->
<bean id="readyDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.ready.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.ready.url}" />
<property name="user" value="${jdbc.ready.username}" />
<property name="password" value="${jdbc.ready.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean>
<!-- write only DataSource -->
<bean id="writeDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.write.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.write.url}" />
<property name="user" value="${jdbc.write.username}" />
<property name="password" value="${jdbc.write.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean>
<!-- 动态路由数据库连接 ,默认是读数据库 -->
<bean id="mySqlDataSource" class="com.hysoft.common.DynamicDataSource">
<property name="targetDataSources">
<map>
<entry key="writeDataSource" value-ref="writeDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="readyDataSource"/>
</bean>
<!-- sessionFactory 将spring和mybatis整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mySqlDataSource" />
<!-- 自动扫描xml目录,省掉手工配置 -->
<property name="mapperLocations" value="classpath:mapping/*/*.xml" />
<property name="plugins">
<list>
<ref bean="pageInterceptor" />
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hysoft.*.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- AOP事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mySqlDataSource" />
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans> 4、在serverice层指出使用的数据源
public Map<String, Object> getAllUserByPage(Map<String, Object> paramMap, String rows, String page){
//主库 读 (默认,可以不写)
JdbcContextHolder.setReadyDataSource();
//辅库 写
// JdbcContextHolder.setWriteDataSource();
Map<String, Object> resultMap = new HashMap<String, Object>();
PageParameter mypage = getPageMap(paramMap, Integer.valueOf(page), Integer.valueOf(rows));
List<UserInfoDomain> rtnList = userInfoMapper.getAllUserByPage(paramMap);
resultMap.put("total", mypage.getTotalCount());
resultMap.put("rows", rtnList);
return resultMap;
}
/**
* 新增数据
* @param view
*/
public void saveUser(UserInfoDomain domain){
JdbcContextHolder.setWriteDataSource();
this.userInfoMapper.insert(domain);
}
/**
* 编辑数据
* @param view
*/
public void updateUser(UserInfoDomain domain){
JdbcContextHolder.setWriteDataSource();
this.userInfoMapper.updateByPrimaryKeySelective(domain);
}