关于spring+springMvc+mybatis整合,配置多数据源

spring整合mybatis配置文件部分

<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”> <context:property-placeholder location="classpath:database/jdbc.properties"ignore-unresolvable=“true” /> <aop:aspectj-autoproxy expose-proxy=“true” /> <bean id=“dataSourceSqlServer” class=“com.alibaba.druid.pool.DruidDataSource"init-method=“init” destroy-method=“close”> <property name=“maxPoolPreparedStatementPerConnectionSize"value=” d r u i d . m a x P o o l P r e p a r e d S t a t e m e n t P e r C o n n e c t i o n S i z e &quot; / &gt; &lt; ! − − 配 置 监 控 统 计 拦 截 的 f i l t e r s − − &gt; &lt; p r o p e r t y n a m e = &quot; f i l t e r s &quot; v a l u e = &quot; {druid.maxPoolPreparedStatementPerConnectionSize}&quot; /&gt;&lt;!-- 配置监控统计拦截的filters --&gt;&lt;property name=&quot;filters&quot; value=&quot; druid.maxPoolPreparedStatementPerConnectionSize"/><!filters><propertyname="filters"value="{druid.filters}” /> <bean id=“dataSourceMySql” class=“com.alibaba.druid.pool.DruidDataSource"init-method=“init” destroy-method=“close”> <property name=“maxPoolPreparedStatementPerConnectionSize"value=” d r u i d . m a x P o o l P r e p a r e d S t a t e m e n t P e r C o n n e c t i o n S i z e &quot; / &gt; &lt; ! − − 配 置 监 控 统 计 拦 截 的 f i l t e r s − − &gt; &lt; p r o p e r t y n a m e = &quot; f i l t e r s &quot; v a l u e = &quot; {druid.maxPoolPreparedStatementPerConnectionSize}&quot; /&gt;&lt;!-- 配置监控统计拦截的filters --&gt;&lt;property name=&quot;filters&quot; value=&quot; druid.maxPoolPreparedStatementPerConnectionSize"/><!filters><propertyname="filters"value="{druid.filters}” /><bean id="dataSourceProxy"class=“org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy”>









<bean id="transactionManager"class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”> <tx:advice id=“txAdvice” transaction-manager=“transactionManager”>tx:attributes<tx:method name=“add*” propagation=“REQUIRED” read-only=“false” rollback-for=“java.lang.Exception” /><tx:method name=“insert*” propagation=“REQUIRED” read-only=“false” rollback-for=“java.lang.Exception” /><tx:method name=“update*” propagation=“REQUIRED” read-only=“false” rollback-for=“java.lang.Exception” /><tx:method name=“delete*” propagation=“REQUIRED” read-only=“false” rollback-for=“java.lang.Exception” /></tx:attributes></tx:advice>

aop:config
<aop:aspect ref=“dataSourceAspect”>

<aop:pointcut id=“dataSourcePointcut” expression=“execution(* com.jjmc.dcl.service.impl..(…))”/>
<aop:before pointcut-ref=“dataSourcePointcut” method=“intercept” />
</aop:aspect>
</aop:config>

除了配置spring,还需要另外再加三个类用来切换数据源
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
*

  • @author huangtao
  • 2017年10月16日下午12:08:10
  • business-server
  • @parameter
  • TODO

*/
public class DynamicDataSource extends AbstractRoutingDataSource{@Overrideprotected Object determineCurrentLookupKey() {// 从自定义的位置获取数据源标识return DynamicDataSourceHolder.getDataSource();}
}

/**
*

  • @author huangtao
  • 2017年10月16日下午12:09:08
  • business-server
  • @parameter
  • TODO

/
public class DynamicDataSourceHolder { /
* * 注意:数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰 */ private static final ThreadLocal THREAD_DATA_SOURCE = new ThreadLocal(); 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(); }
}

import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import com.jjmc.dcl.service.DataSource;
/**
*

  • @author huangtao
  • 2017年10月16日下午12:20:49
  • business-server
  • @parameter
  • TODO

/
public class DynamicDataSourceInterceptor {
// public void setdataSourceMysql(JoinPoint jp)
// {
// DynamicDataSourceHolder.setDataSource(“dataSourceSqlServer”);
// }
//
// public void setdataSourceSql(JoinPoint jp) {
// DynamicDataSourceHolder.setDataSource(“dataSourceMySql”);
// }
// public void intercept(JoinPoint point) throws Exception { Class<?> target = point.getTarget().getClass(); MethodSignature signature = (MethodSignature) point.getSignature(); // 默认使用目标类型的注解,如果没有则使用其实现接口的注解 for (Class<?> clazz : target.getInterfaces()) { resolveDataSource(clazz, signature.getMethod()); } resolveDataSource(target, signature.getMethod()); } /
* * 提取目标对象方法注解和类型注解中的数据源标识 * * @param clazz * @param method / private void resolveDataSource(Class<?> clazz, Method method) { try { Class<?>[] types = method.getParameterTypes(); // 默认使用类型注解 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) { System.out.println(clazz + “:” + e.getMessage()); } }
}
注意:第三个类中注释掉的代码是手动切换,没注释的代码是注解识别,注解可以定义在serviceImpl和dao的方法上,这个项目是定义在serviceImpl的方法上。
如果要手动切换数据源,那么需要DynamicDataSourceHolder.setDataSource(“dataSourceSqlServer”);写在java代码中,进行切换数据源。
最后,再定义一个注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
/
*
*

  • @author huangtao
  • 2017年10月16日下午12:57:23
  • business-server
  • @parameter
  • TODO
  • 自定义注解DataSource
    */
    @Target(value = { ElementType.TYPE,ElementType.METHOD })
    @Retention(value = RetentionPolicy.RUNTIME)
    public @interface DataSource {String value();
    }

这样就完成了注解自动切换数据源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值