mysqldb.properties
#主数据库数据源
jdbc_url=jdbc:mysql://localhost:3306/demo?useUnicode=true&allowMultiQueries=true
jdbc_username=root
jdbc_password=sa123456
validationQuery=SELECT 1
#从数据库数据源
slave_jdbc_url=jdbc:mysql://localhost:6603/demo?useUnicode=true&allowMultiQueries=true
spring-mybatis.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置主库数据源 -->
<bean name="masterdruidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<property name="initialSize" value="0" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="mergeStat" />
</bean>
<!-- 配置从库数据源 -->
<bean name="slavedruidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${slave_jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<property name="initialSize" value="0" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="mergeStat" />
</bean>
<!-- 动态数据源,根据service接口上的注解来决定取哪个数据源 -->
<bean id="dataSource" class="com.iflytek.utils.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<!-- write or slave -->
<entry key="slave" value-ref="slavedruidDataSource"/>
<!-- read or master -->
<entry key="master" value-ref="masterdruidDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="masterdruidDataSource"/>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- <property name="configLocation" value="classpath:mybatis/mybatis.xml" /> -->
<property name="mapperLocations" value="classpath:core_resources/*.xml" />
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage">
<value>
com.iflytek.business.model
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- enable transaction demarcation with annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- enable transaction demarcation with annotations -->
<tx:annotation-driven />
<!-- 为业务逻辑层的方法解析@DataSource注解 为当前线程的HandleDataSource注入数据源 -->
<bean id="dataSourceAspect" class="com.iflytek.utils.DataSourceAspect" />
<aop:config proxy-target-class="true">
<aop:aspect id="dataSourceAspect" ref="dataSourceAspect" order="2">
<aop:pointcut id="tx" expression="execution(* com.iflytek.business.service.impl..*.*(..)) "/>
<aop:before pointcut-ref="tx" method="before" />
</aop:aspect>
</aop:config>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage">
<value>
com.iflytek.business.dao
</value>
</property>
</bean>
</beans>
AOP实现数据源的动态切换
DataSource.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* RUNTIME
* 编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource
{
String value();
}
DataSourceAspect.java
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
public class DataSourceAspect
{
/**
* 在dao层方法获取datasource对象之前,在切面中指定当前线程数据源
*/
public void before(JoinPoint point)
{
Object target = point.getTarget();
String method = point.getSignature().getName();
Class<?>[] classz = target.getClass().getInterfaces(); // 获取目标类的接口, 所以@DataSource需要写在接口上
Class<?>[] parameterTypes = ((MethodSignature) point.getSignature())
.getMethod().getParameterTypes();
try
{
Method m = classz[0].getMethod(method, parameterTypes);
if (m != null && m.isAnnotationPresent(DataSource.class))
{
DataSource data = m.getAnnotation(DataSource.class);
System.out.println("用户选择数据库库类型:" + data.value());
HandleDataSource.putDataSource(data.value()); // 数据源放到当前线程中
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
DynamicDataSource.java
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource
{
/**
* 获取与数据源相关的key 此key是Map<String,DataSource> resolvedDataSources 中与数据源绑定的key值
* 在通过determineTargetDataSource获取目标数据源时使用
*/
@Override
protected Object determineCurrentLookupKey()
{
return HandleDataSource.getDataSource();
}
}
HandleDataSource.java
public class HandleDataSource
{
public static final ThreadLocal<String> holder = new ThreadLocal<String>();
/**
* 绑定当前线程数据源
*
* @param key
*/
public static void putDataSource(String datasource)
{
holder.set(datasource);
}
/**
* 获取当前线程的数据源
*
* @return
*/
public static String getDataSource()
{
return holder.get();
}
}
service接口上应用@DataSource实现数据源的指定
import java.util.List;
import com.yzb.model.Person;
import com.yzb.util.DataSource;
public interface IPersonService {
/**
* 加载全部的person
* @return
*/
List<Person> listAllPerson();
/**
* 查询某个人的信息
* @param personId
* @return
*/
@DataSource("slave") // 指定使用从数据源
Person getPerson(int personId);
boolean updatePerson(Person person);
}