Spring多数据源配置(干货)

1.配置多数据源连接信息(jdbc.properties)

  • jdbc.properties
# oracle 连接信息
jdbc.oracle.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.oracle.url=jdbc:oracle:thin:@127.0.0.1:6694/{database}
jdbc.oracle.username={userName}
jdbc.oracle.password={passwork}
# mysql 连接信息
jdbc.mysql.driver=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://127.0.0.1:3306/{database}
jdbc.mysql.username={userName}
jdbc.mysql.password={passwork}

2.配置xml文件(springmvc.xml)

#oracle

        
        
        
        
        

#mysql

        
        
        
        

#自定义动态数据源实体配置

        
        
            
                
                
            
        
    

3.创建动态数据源配置类

package cn.cxqnet.datasource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
 * @author MyIreland
 * @description 动态数据源实体
 * @date 2018/10/19
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    public static final String ORACLE_DATASOURCE = "oracleDataSource";

    public static final String MY_SQL_DATASOURCE = "mysqlDataSource";

    //本地线程,获取当前正在执行的currentThread
    public static final ThreadLocal contextHolder = new ThreadLocal();

    public static void setCurrentDatasource(String dataSourceType){
        contextHolder.set(dataSourceType);
    }

    public static String getCurrentDatasource(){
        return contextHolder.get();
    }

    public static void clearCurrentDatasource(){
        contextHolder.remove();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return getCurrentDatasource();
    }
}

4.创建数据源注解

package cn.cxqnet.datasource.annotation;

import java.lang.annotation.*;

/**
 * @author MyIreland
 * @description 动态数据源注解
 * @date 2018/10/19
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
    String value() default "";
}

5.创建动态数据源切面(注解方式)

package cn.cxqnet.datasource.aspect;

import cn.cxqnet.datasource.DynamicDataSource;
import cn.cxqnet.datasource.annotation.DataSource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
 * @author MyIreland
 * @description 动态数据源切面
 * @date 2018/10/19
 */
@Aspect
@Component
public class DataSourceAspect {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Pointcut("@annotation(cn.xdf.amoy.datasource.annotation.DataSource)")
    public void dataSourcePointCut() {
    }
    @Around("dataSourcePointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        DataSource ds = method.getAnnotation(DataSource.class);
        if(ds == null){
            DynamicDataSource.setCurrentDatasource(DynamicDataSource.ORACLE_DATASOURCE);
            logger.debug("set datasource is {}", DynamicDataSource.ORACLE_DATASOURCE);
        }else {
            DynamicDataSource.setCurrentDatasource(DynamicDataSource.MY_SQL_DATASOURCE);
            logger.debug("set datasource is {}", DynamicDataSource.MY_SQL_DATASOURCE);
        }
        try {
            return point.proceed();
        } finally {
            DynamicDataSource.clearCurrentDatasource();
            logger.debug("clean datasource");
        }
    }
}

6.使用方式

在serviceImpl的方法里面添加@DataSource注解即可

    @Override
    @DataSource("{数据库名字}")
    public Object save( CmdbAsset asset ) {
        // 保存设备时,需要执行一步对设备进行编码的操作。
        String something = dao.doSomething();
    }

这样就简单的实现了数据源的配置,得到帮助的补充详细的点哈,springBoot的配置则将其原理转到Class的配置加上@Configuration,写上@Bean方法配置相关参数即可

你可能感兴趣的:(Spring多数据源配置(干货))