springboot配置多数据源(静态和动态数据源)

 更新时间:2023年09月27日 11:51:26   作者:随风丶飘  
在开发过程中,很多时候都会有垮数据库操作数据的情况,需要同时配置多套数据源,本文主要介绍了springboot配置多数据源(静态和动态数据源),感兴趣的可以了解一下

背景

在开发过程中,很多时候都会有垮数据库操作数据的情况,需要同时配置多套数据源,即多个数据库,保证不同的业务在不同的数据库执行操作,通过mapper来灵活的切换数据源。

本文以sqlserver和mysql混合数据源配置为例。

配置多数据源方案

1、通过mapper配置数据源

2、配置动态数据源

具体实现

1)、 通过mapper配置数据源

(1)maven配置

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

(2)服务配置文件,application.yml

server:
  port: 9900
spring:
  datasource:
    db1:
      type: com.zaxxer.hikari.HikariDataSource
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url: jdbc:sqlserver://localhost:10009;DatabaseName=test
      username: sa
      password: 654321
    db2:
      type: com.zaxxer.hikari.HikariDataSource
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/test2?useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowMultiQueries=true
      username: root
      password: 123456

(3)添加数据库配置

@Configuration
public class DataSourceConfig {
    @Bean(name = "maindb")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource businessDbDataSource() {
        return new HikariDataSource();
    }
    @Bean(name = "seconddb")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource newhomeDbDataSource() {
        return new HikariDataSource();
    }
}

(4)单独配置每个数据源信息

注意:@Primary该注解理解为默认数据源

包路径配置可对比参考后面的项目结构截图

@Configuration
@MapperScan(basePackages = {"com.gxin.datasource.dao.maindb"}, sqlSessionFactoryRef = "sqlSessionFactoryMaindb")
public class DatasourceMainConfig {
    @Autowired
    @Qualifier("maindb")
    private DataSource dataSourceMaindb;
    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactoryMaindb() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceMaindb);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/maindb/*.xml"));
        // 打印sql日志
        // org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        // configuration.setLogImpl(StdOutImpl.class);
        // factoryBean.setConfiguration(configuration);
        return factoryBean.getObject();
    }
    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplateMaindb() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryMaindb());
    }
}
@Configuration
@MapperScan(basePackages = {"com.gxin.datasource.dao.seconddb"}, sqlSessionFactoryRef = "sqlSessionFactorySeconddb")
public class DatasourceSecondConfig {
    @Autowired
    @Qualifier("seconddb")
    private DataSource dataSourceSeconddb;
    @Bean
    public SqlSessionFactory sqlSessionFactorySeconddb() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceSeconddb);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/seconddb/*.xml"));
        // 打印sql日志
        // org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        // configuration.setLogImpl(StdOutImpl.class);
        // factoryBean.setConfiguration(configuration);
        return factoryBean.getObject();
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplateSeconddb() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactorySeconddb());
    }
}

(5)根据每个单独的数据源的配置信息搭建mapper接口和mapper.xml文件

(6)配置mapper多数据源完成,下图为完整的项目结构

2)、 配置动态数据源

(1)maven配置

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

(2)服务配置文件,application.yml

server:
  port: 9901
spring:
  main:
    allow-bean-definition-overriding: true
  datasource:
    db1:
      type: com.zaxxer.hikari.HikariDataSource
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url: jdbc:sqlserver://localhost:10009;DatabaseName=test
      username: sa
      password: 654321
    db2:
      type: com.zaxxer.hikari.HikariDataSource
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/test2?useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowMultiQueries=true
      username: root
      password: 123456

(3)添加数据源列表

public interface DataSourceConstant {
    /**
     * 默认数据库
     */
    String MAIN = "MAIN";
    /**
     * 第二数据库
     */
    String SECOND = "SECOND";
}

(4)动态数据源数据库信息配置

@Configuration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public class DynamicDataSourceConfig {
    // 核心数据库
    @Bean(name = DataSourceConstant.MAIN)
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource getMAINDataSource() {
        return new HikariDataSource();
    }
    // 第二数据库
    @Bean(name = DataSourceConstant.SECOND)
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource getSECONDDataSource() {
        return new HikariDataSource();
    }
    @Bean
    @Primary
    public DataSource dynamicDataSource() {
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put(DataSourceConstant.MAIN, getMAINDataSource());
        dataSourceMap.put(DataSourceConstant.SECOND, getSECONDDataSource());
        //设置动态数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        dynamicDataSource.setDefaultTargetDataSource(getMAINDataSource());
        return dynamicDataSource;
    }
}

(5)添加动态数据源策略获取配置,继承AbstractRoutingDataSource

@Component
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        // 此处暂时返回固定 master 数据源, 后面按动态策略修改
        return DynamicDataSourceContextHolder.getContextKey();
    }
}

(6)数据源切换策略配置

public class DynamicDataSourceContextHolder {
    /**
     * 动态数据源名称上下文
     */
    private static final ThreadLocal<String> DATASOURCE_CONTEXT_KEY_HOLDER = new ThreadLocal<>();
    /**
     * 设置/切换数据源
     */
    public static void setContextKey(String key) {
        DATASOURCE_CONTEXT_KEY_HOLDER.set(key);
    }
    /**
     * 获取数据源名称
     */
    public static String getContextKey() {
        String key = DATASOURCE_CONTEXT_KEY_HOLDER.get();
        return key == null ? DataSourceConstant.MAIN : key;
    }
    /**
     * 删除当前数据源名称
     */
    public static void removeContextKey() {
        DATASOURCE_CONTEXT_KEY_HOLDER.remove();
    }
}

(6.1) 这里写掉了,补充数据库会话配置

@Configuration
public class SqlSessionConfig {
    @Autowired
    private DynamicDataSource source;
    public SqlSessionConfig() {
    }
    @Bean(name = {"sqlSessionFactoryBean"})
    public SqlSessionFactoryBean getSessionFactory() throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(this.source);
        factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        // 打印sql日志
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLogImpl(StdOutImpl.class);
        factory.setConfiguration(configuration);
        return factory;
    }
    @Bean(name = {"sqlSession"})
    public SqlSessionTemplate getSqlSession() throws Exception {
        return new SqlSessionTemplate(Objects.requireNonNull(getSessionFactory().getObject()));
        // SqlSessionFactory factory = this.getSessionFactory().getObject();
        // return new SqlSessionTemplate(factory, ExecutorType.BATCH);
    }
    @Bean
    public DataSourceTransactionManager getDataSourceTransaction() {
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(this.source);
        return manager;
    }
}

(7) 添加切换数据源标识注解,默认为MAIN数据源

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DBS {
    /**
     * 数据源名称
     */
    String value() default DataSourceConstant.MAIN;
}

(8)通过配置的数据源标识注解,动态切换数据源

@Aspect
@Component
public class DynamicDataSourceAspect {
    @Pointcut("@annotation(com.gxin.dynamicdatasource.config.DBS)")
    public void dataSourcePointCut() {
    }
    @Around("dataSourcePointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String dsKey = getDSAnnotation(joinPoint).value();
        DynamicDataSourceContextHolder.setContextKey(dsKey);
        try {
            return joinPoint.proceed();
        } finally {
            DynamicDataSourceContextHolder.removeContextKey();
        }
    }
    /**
     * 根据类或方法获取数据源注解
     */
    private DBS getDSAnnotation(ProceedingJoinPoint joinPoint) {
        Class<?> targetClass = joinPoint.getTarget().getClass();
        DBS dsAnnotation = targetClass.getAnnotation(DBS.class);
        // 先判断类的注解,再判断方法注解
        if (Objects.nonNull(dsAnnotation)) {
            return dsAnnotation;
        } else {
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            return methodSignature.getMethod().getAnnotation(DBS.class);
        }
    }
}

(9)到此动态数据源配置完成,一下的为使用情况,需要使用哪个数据源添加注解配置即可

@Repository
public interface TestMapper {
    int getModelcount();
    @DBS(DataSourceConstant.SECOND)
    int getUsercount();
}

(10)service层

@Service
public class ServiceInfo {
    @Autowired
    private TestMapper mainMapper;
    public void datasource() {
        int modelcount = mainMapper.getModelcount();
        System.out.println(modelcount);
        int usercount = mainMapper.getUsercount();
        System.out.println(usercount);
    }
}

(11)mapper. xml结构目录

(12)完整的项目结构截图

到此这篇关于springboot配置多数据源(静态和动态数据源)的文章就介绍到这了,更多相关springboot 多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Java线程池如何实现优雅退出

    详解Java线程池如何实现优雅退出

    这篇文章我们将从源码角度深度解析线程池是如何优雅的退出程序的,文中的示例代码讲解详细,对我们学习java线程池有一定帮助,需要的可以参考一下
    2022-07-07
  • java反射之通过反射了解集合泛型的本质(详解)

    java反射之通过反射了解集合泛型的本质(详解)

    下面小编就为大家带来一篇java反射之通过反射了解集合泛型的本质(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Java enum关键字不识别的快速解决办法

    Java enum关键字不识别的快速解决办法

    这篇文章主要介绍了Java enum关键字不识别的快速解决办法,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-09-09
  • javaGUI实现多人聊天功能

    javaGUI实现多人聊天功能

    这篇文章主要为大家详细介绍了javaGUI实现多人聊天功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • springboot幂等切片的实现

    springboot幂等切片的实现

    本文主要介绍了springboot幂等切片的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Java利用TreeUtils工具类实现列表转树

    Java利用TreeUtils工具类实现列表转树

    在开发过程中,总有列表转树的需求,几乎是项目的标配,有没有一种通用且跨项目的解决方式呢?本文将基于Java8的Lambda 表达式和Stream等知识,使用TreeUtils工具类实现一行代码完成列表转树这一通用型需求,需要的可以参考一下
    2022-11-11
  • jstl EL表达式遍历Map的方法

    jstl EL表达式遍历Map的方法

    这篇文章主要介绍了jstl EL表达式遍历Map的方法的相关资料,需要的朋友可以参考下
    2016-12-12
  • mybatis模糊查询之bind标签和concat函数用法详解

    mybatis模糊查询之bind标签和concat函数用法详解

    大家都知道bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中,接下来通过本文给大家介绍了mybatis模糊查询——bind标签和concat函数用法,需要的朋友可以参考下
    2022-08-08
  • Spring Boot中@Conditional注解介绍

    Spring Boot中@Conditional注解介绍

    @Conditional表示仅当所有指定条件都匹配时,组件才有资格注册。该@Conditional注释可以在以下任一方式使用:作为任何@Bean方法的方法级注释、作为任何类的直接或间接注释的类型级别注释@Component,包括@Configuration类、作为元注释,目的是组成自定义构造型注释
    2022-09-09
  • spring利用squertz实现定时任务

    spring利用squertz实现定时任务

    spring squertz是一个强大的定时任务处理方式。下面这篇文章主要介绍了spring利用squertz实现定时任务的相关资料,文中介绍的很详细,对大家具有一定的参考借鉴价值,需要的朋友们下面来一起看看吧。
    2017-01-01

最新评论