mybatis 整合多数据源配置

1. jdbc中配置多数据源

<bean id="dynamicDataSource" class="com.fwone.dynamicDataSource.DynamicDataSource">
   <property name="targetDataSources">
      <map key-type="java.lang.String">
         <entry value-ref="dataSource" key="dataSource1"></entry>
         <entry value-ref="dataSource2" key="dataSource2"></entry>
      </map>
   </property>
   <property name="defaultTargetDataSource" ref="dataSource"></property>
</bean>

2. mybatis中配置dataSource为动态数据源

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="configLocation" value="classpath:mybatis.xml" />
   <property name="dataSource" ref="dynamicDataSource" />
</bean>

3. 增加DataSource注解类

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@Documented
public @interface DataSource {
    String dataSource() default "default";
}

4.创建dynamicDataSource类实现DataSource接口

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

    private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();

    public static void setDataSourceKey(String dataSource){
        dataSourceKey.set(dataSource);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return dataSourceKey.get();
    }
}

5. 创建处理@DataSource注解AOP


import com.fwone.annotation.DataSource;
import com.fwone.dynamicDataSource.DynamicDataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class DataSourceAspect {

    //配置接入点
    @Pointcut("@annotation(com.fwone.annotation.DataSource)")
    private void controllerAspect(){}//定义一个切入点

    @Before("controllerAspect()")
    public void dataSwitch(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature =(MethodSignature) signature;
        Method method = methodSignature.getMethod();
        DataSource data = null;
        String dataSource = null;

        if(method != null){
            data = method.getAnnotation(DataSource.class);
            // 方法上的注解
            if(data != null){
                dataSource = data.dataSource();
                if(dataSource != null){
                    DynamicDataSource.setDataSourceKey(dataSource);
                }
            }else{
                // 获取类上的注解
                data = joinPoint.getTarget().getClass().getAnnotation(DataSource.class);
                if(data!=null){
                    dataSource = data.dataSource();
                    if(dataSource != null){
                        DynamicDataSource.setDataSourceKey(dataSource);
                    }
                }else{
                    // 默认为default数据源
                    DynamicDataSource.setDataSourceKey("ferpt");
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值