先上xml配置 ps:實(shí)際上你可以選擇java注解的方式
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean name="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:39000/apptest?useUnicode=true" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 動(dòng)態(tài)數(shù)據(jù)源配置侥祭,這個(gè)class要實(shí)現(xiàn) -->
<bean id="dynamicDataSource"
class="com.data.util.jdbcutil.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<!-- 指定lookupKey和與之對(duì)應(yīng)的數(shù)據(jù)源,切換時(shí)使用的為key -->
<entry key="dataSource1" value-ref="dataSource"></entry>
<entry key="dataSource2" value-ref="dataSource2"></entry>
</map>
</property>
<!-- 這里可以指定默認(rèn)的數(shù)據(jù)源 -->
<property name="defaultTargetDataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<!-- 自動(dòng)掃描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/*/mapping/**/*.xml">
</property>
<property name="configLocation" value="classpath:mybatis-config.xml">
</property>
</bean>
<bean id="dataSourceAspect" class="com.data.util.jdbcutil.DataSourceAspect" />
<aop:config>
<aop:aspect ref="dataSourceAspect">
<!-- 攔截所有service方法,切面插入攔截的方法录淡,獲取注解-->
<aop:pointcut id="dataSourcePointcut" expression="@annotation(com.data.util.jdbcutil.DataSource)" />
<aop:around pointcut-ref="dataSourcePointcut" method="intercept" />
</aop:aspect>
</aop:config>
如圖所示四個(gè)類實(shí)現(xiàn)了切換數(shù)據(jù)源
DataSource ---注解绩聘,實(shí)現(xiàn)標(biāo)記功能
DataSourceAspect ---實(shí)現(xiàn)切換數(shù)據(jù)源功能
DynamicDataSource --- 實(shí)現(xiàn)AbstractRoutingDataSource的模板方法
DynamicDataSourceHolder --- 使用線程儲(chǔ)存數(shù)據(jù)源的key
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
String value();
}
public class DataSourceAspect {
/**
* 攔截目標(biāo)方法,獲取由@DataSource指定的數(shù)據(jù)源標(biāo)識(shí),設(shè)置到線程存儲(chǔ)中以便切換數(shù)據(jù)源
*
* @param point
* @throws Exception
*/
public Object intercept(ProceedingJoinPoint point) {
try{
Class<?> target = point.getTarget().getClass();
MethodSignature signature = (MethodSignature) point.getSignature();
// 默認(rèn)使用目標(biāo)類型的注解屹培,如果沒有則使用其實(shí)現(xiàn)接口的注解
for (Class<?> clazz : target.getInterfaces()) {
resolveDataSource(clazz, signature.getMethod());
}
resolveDataSource(target, signature.getMethod());
return point.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
} finally {
DynamicDataSourceHolder.clearDataSource();
}
return null;
}
/**
* 提取目標(biāo)對(duì)象方法注解和類型注解中的數(shù)據(jù)源標(biāo)識(shí)
*
* @param clazz
* @param method
*/
private void resolveDataSource(Class<?> clazz, Method method) {
try {
Class<?>[] types = method.getParameterTypes();
// 默認(rèn)使用類型注解
if (clazz.isAnnotationPresent(DataSource.class)) {
DataSource source = clazz.getAnnotation(DataSource.class);
DynamicDataSourceHolder.setDataSource(source.value());
}
// 方法注解可以覆蓋類型注解
Method m = clazz.getMethod(method.getName(), types);
if (m != null && m.isAnnotationPresent(DataSource.class)) {
DataSource source = m.getAnnotation(DataSource.class);
DynamicDataSourceHolder.setDataSource(source.value());
}
} catch (Exception e) {
}
}
}
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSource();
}
}
public class DynamicDataSourceHolder {
/**
* 注意:數(shù)據(jù)源標(biāo)識(shí)保存在線程變量中轴总,避免多線程操作數(shù)據(jù)源時(shí)互相干擾
*/
private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<String>();
public static String getDataSource() {
return THREAD_DATA_SOURCE.get();
}
public static void setDataSource(String dataSource) {
THREAD_DATA_SOURCE.set(dataSource);
}
public static void clearDataSource() {
THREAD_DATA_SOURCE.remove();
}
}
使用案例
@Autowired
private MXGLMapper mapper;
@DataSource("dataSource2")
public List<Map<String,Object>> getmodel_table_prvd(){
return mapper.getList();
}
@DataSource("dataSource")
public List<Map<String, Object>> getmodel_table_prvd1() {
return mapper.getList();
}