一、配置类
DataSourceContextHolder.java
package com.jiefupay.database;
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
//设置当前Thread的数据库类型
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
//获取当前Thread的数据库类型
public static String getDbType() {
return ((String) contextHolder.get());
}
public static void clearDbType() {
contextHolder.remove();
}
}
package com.jiefupay.database;
/**
* 数据库名称常量类
*/
public class DataSourceName {
public static final String WRITE_SOURCE = "mysql_write_source";
public static final String READ_SOURCE = "mysql_read_source";
}
package com.jiefupay.database;
import java.util.logging.Logger;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
public Logger getParentLogger() {
return null;
}
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDbType();
}
}
package com.jiefupay.datebase;
import org.aspectj.lang.ProceedingJoinPoint;
public class ChangDBInterceptor {
private static final String[] method = {"query" , "list", "get", "search", "find"};
public Object changeDB(ProceedingJoinPoint pjp) throws Throwable {
List<String> aList = Arrays.asList(method);
String methodName = pjp.getSignature().getName();
if (search(methodName,aList)){
LOG.info("---------READ DATASOURCE --------" + DataSourceName.READ_SOURCE);
DataSourceContextHolder.setDbType(DataSourceName.READ_SOURCE);
}else{
LOG.info("---------WRITE DATASOURCE --------" + DataSourceName.READ_SOURCE);
DataSourceContextHolder.setDbType(DataSourceName.WRITE_SOURCE);
}
return pjp.proceed();
}
/**
* 模糊查询
*/
public static boolean search(String name, List<String> list) {
for (int i = 0; i < list.size(); i++) {
Pattern pattern = Pattern.compile(list.get(i),Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(name);
if(matcher.find()){
return true;
}
}
return false;
}
}
二、jdbc.properties文件如下:
connection.read.username=只读账号
connection.read.password=只读密码
connection.read.url=jdbc:mysql://127.0.0.1:3306/数据库
connection.read.driverClassName=com.mysql.jdbc.Driver
connection.read.initialSize=10
connection.read.maxActive=100
connection.read.maxIdle=50
connection.read.minIdle=5
connection.read.maxWait=5000
connection.read.removeAbandoned=true
connection.read.removeAbandonedTimeout=3000
connection.read.logAbandoned=false
connection.read.timeBetweenEvictionRunsMillis=3600000
connection.read.testWhileIdle=true
connection.read.validationQuery=select 1 from dual
connection.write.username=root
connection.write.password=12345678
connection.write.url=jdbc:mysql://127.0.0.1:3306/数据库
connection.write.driverClassName=com.mysql.jdbc.Driver
connection.write.initialSize=10
connection.write.maxActive=100
connection.write.maxIdle=50
connection.write.minIdle=5
connection.write.maxWait=5000
connection.write.removeAbandoned=true
connection.write.removeAbandonedTimeout=3000
connection.write.logAbandoned=false
connection.write.timeBetweenEvictionRunsMillis=3600000
connection.write.testWhileIdle=true
connection.write.validationQuery=select 1 from dual
三、applicationContext.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"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<bean id="mysql_read_source" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${connection.read.username}"></property>
<property name="password" value="${connection.read.password}"></property>
<property name="url" value="${connection.read.url}"></property>
<property name="driverClassName" value="${connection.read.driverClassName}"></property>
<property name="maxActive" value="${connection.read.maxActive}"></property>
<property name="maxIdle" value="${connection.read.maxIdle}"></property>
<property name="minIdle" value="${connection.read.minIdle}"></property>
<property name="maxWait" value="${connection.read.maxWait}"></property>
<property name="removeAbandoned" value="${connection.read.removeAbandoned}"></property>
<property name="removeAbandonedTimeout" value="${connection.read.removeAbandonedTimeout}"></property>
<property name="logAbandoned" value="${connection.read.logAbandoned}"></property>
<property name="timeBetweenEvictionRunsMillis">
<value>3600000</value><!--1 hours-->
</property>
<property name="testWhileIdle">
<value>true</value>
</property>
<property name="validationQuery">
<value>select 1 from dual</value>
</property>
</bean>
<bean id="mysql_write_source" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${connection.write.username}"></property>
<property name="password" value="${connection.write.password}"></property>
<property name="url" value="${connection.write.url}"></property>
<property name="driverClassName" value="${connection.write.driverClassName}"></property>
<property name="maxActive" value="${connection.write.maxActive}"></property>
<property name="maxIdle" value="${connection.write.maxIdle}"></property>
<property name="minIdle" value="${connection.write.minIdle}"></property>
<property name="maxWait" value="${connection.write.maxWait}"></property>
<property name="removeAbandoned" value="${connection.write.removeAbandoned}"></property>
<property name="removeAbandonedTimeout" value="${connection.write.removeAbandonedTimeout}"></property>
<property name="logAbandoned" value="${connection.write.logAbandoned}"></property>
<property name="timeBetweenEvictionRunsMillis">
<value>3600000</value><!--1 hours-->
</property>
<property name="testWhileIdle">
<value>true</value>
</property>
<property name="validationQuery">
<value>select 1 from dual</value>
</property>
</bean>
<!-- 动态配置数据源 -->
<bean id ="dataSource" class= "com.database.DynamicDataSource" >
<property name ="targetDataSources">
<map key-type ="java.lang.String">
<entry value-ref ="mysql_read_source" key="mysql_read_source"></entry >
<entry value-ref ="mysql_write_source" key="mysql_write_source"></entry >
</map>
</property >
<property name ="defaultTargetDataSource" ref= "mysql_write_source"></property >
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="changeDBInterceptor" class="com.*.database.ChangDBInterceptor"></bean>
<aop:config proxy-target-class="true">
<aop:pointcut id="txPointcut" expression="execution(* com.包名..*.*.service..*.*(..))" />
<aop:aspect ref="changeDBInterceptor" order="1">
<aop:around pointcut-ref="txPointcut" method="changeDB"/>
</aop:aspect>
</aop:config>
</beans>
测试成功
只读如下
写入如下