SpringBoot+Druid+Mybatis-plus多数据源配置

1. mybatisConfig:

package com.boss.xm.fic.xmficriskcontrolmanage.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.boss.xm.fic.common.handler.MetaHandler;
import com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic.DataSourceEnum;
import com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic.MultipleDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Mybatis-Plus Config
 *
 * @author hero
 * @date 2021/10/21 11:08
 */
@Configuration
public class MybatisPlusConfigRiskControl {

    @Bean("mybatisPlusInterceptorRiskControl")
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 还可以添加其他的拦截器
        return interceptor;
    }

    @Bean("globalConfigRiskControl")
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setTablePrefix("fic_");
        globalConfig.setDbConfig(dbConfig);
        globalConfig.setMetaObjectHandler(new MetaHandler());
        return globalConfig;
    }

    @Bean("db1")
    @ConfigurationProperties(prefix = "spring.datasource.druid.db1")
    public DataSource db1() {
//        return new DruidDataSource();
        return DruidDataSourceBuilder.create().build();
    }

    @Bean("db2")
    @ConfigurationProperties(prefix = "spring.datasource.druid.db2")
    public DataSource db2() {
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 动态数据源配置
     * @return
     */
    @Bean
    @Primary
    public DataSource multipleDataSource(@Qualifier("db1") DataSource db1, @Qualifier("db2") DataSource db2) {
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        Map< Object, Object > targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceEnum.DB1.getValue(), db1);
        targetDataSources.put(DataSourceEnum.DB2.getValue(), db2);
        //添加数据源
        multipleDataSource.setTargetDataSources(targetDataSources);
        //设置默认数据源
        multipleDataSource.setDefaultTargetDataSource(db1);
        return multipleDataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2()));
        //sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*/*Mapper.xml"));
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));

        sqlSessionFactory.setConfiguration(mybatisConfiguration());
        sqlSessionFactory.setPlugins(new Interceptor[]{
                mybatisPlusInterceptor()
        });
        sqlSessionFactory.setGlobalConfig(globalConfig());

        return sqlSessionFactory.getObject();
    }

    @Bean("mybatisConfigurationRiskControl")
    public MybatisConfiguration mybatisConfiguration() {
        MybatisConfiguration configuration = new MybatisConfiguration();
        // 开启返回map结果集的下划线转驼峰
        configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
        configuration.setMapUnderscoreToCamelCase(true);
//        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setCacheEnabled(false);
        return configuration;
    }

}

2.DataSource

package com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic;

import java.lang.annotation.*;

/**
 * 动态数据源注解
 * @author Louis
 * @date Jun 17, 2019
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
    
    /**
     * 数据源key值
     * @return
     */
    DataSourceEnum value();
    
}

3.DataSourceAspect

package com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {

    @Pointcut("@within(com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic.DataSource) || @annotation(com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic.DataSource)")
    public void pointCut() {

    }

    @Before("pointCut() && @annotation(dataSource)")
    public void doBefore(DataSource dataSource) {
        log.info("选择数据源:" + dataSource.value().getValue());
        DataSourceContextHolder.setDataSource(dataSource.value().getValue());
    }

    @After("pointCut()")
    public void doAfter() {
        DataSourceContextHolder.clear();
    }
}

4.DataSourceContextHolder

package com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic;

public class DataSourceContextHolder {

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

    /**
     *  设置数据源
     * @param db
     */
    public static void setDataSource(String db){
        contextHolder.set(db);
    }

    /**
     * 取得当前数据源
     * @return
     */
    public static String getDataSource(){
        return contextHolder.get();
    }

    /**
     * 清除上下文数据
     */
    public static void clear(){
        contextHolder.remove();
    }
}

5.DataSourceEnum

package com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic;

/**
 * 多数据源
 *
 * @author hero
 * @date 2022-03-07
 */
public enum DataSourceEnum {

    DB1("db1"), DB2("db2");

    private String value;

    DataSourceEnum(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

6.MultipleDataSource

package com.boss.xm.fic.xmficriskcontrolmanage.config.dynamic;

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

public class MultipleDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDataSource();
    }
}

7.配置:

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.db1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.db1.jdbc-url=jdbc:mysql://8.142.31.144:3306/internal_control?useUnicode=true&characterEncoding=utf-8
spring.datasource.druid.db1.username=root
spring.datasource.druid.db1.password=6UqIVSFvnPVpnP1
spring.datasource.druid.db1.initialSize=5
spring.datasource.druid.db1.minIdle=5
spring.datasource.druid.db1.maxActive=20
spring.datasource.druid.db1.maxWait=60000
spring.datasource.druid.db1.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.db1.minEvictableIdleTimeMillis=300000
spring.datasource.druid.db1.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.db1.testWhileIdle=true
spring.datasource.druid.db1.testOnBorrow=false
spring.datasource.druid.db1.testOnReturn=false
spring.datasource.druid.db1.poolPreparedStatements=true
spring.datasource.druid.db1.maxPoolPreparedStatementPerConnectionSize=20

#spring.datasource.druid.db2.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.db2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.db2.jdbc-url=jdbc:mysql://8.142.31.144:3306/xm_pay?useUnicode=true&characterEncoding=utf-8
spring.datasource.druid.db2.username=root
spring.datasource.druid.db2.password=6UqIVSFvnPVpnP1
spring.datasource.druid.db2.initialSize=5
spring.datasource.druid.db2.minIdle=5
spring.datasource.druid.db2.maxActive=20
spring.datasource.druid.db2.maxWait=60000
spring.datasource.druid.db2.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.db2.minEvictableIdleTimeMillis=300000
spring.datasource.druid.db2.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.db2.testWhileIdle=true
spring.datasource.druid.db2.testOnBorrow=false
spring.datasource.druid.db2.testOnReturn=false
spring.datasource.druid.db2.poolPreparedStatements=true
spring.datasource.druid.db2.maxPoolPreparedStatementPerConnectionSize=20

你可能感兴趣的:(Java,Spring,java,spring,spring,boot)