Spring boot 2.x 多数据源配置

1.主从数据源配置(读写分离)

参考链接:

https://gitee.com/comven/dynamic-datasource-demo?spm=5176.100239.blogcont188540.13.iARYDh

https://blog.csdn.net/qq_35981283/article/details/78846892

 

2数据库信息需要动态加载(每一个线程根据自己的实际参数设置数据源)

参考链接:

https://blog.csdn.net/c598976342/article/details/82893993

 

(1)pom jar包引入

<!--spring boot web-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

    <!--spring boot mybatis-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.1</version>
    </dependency>

    <!--spring boot AOP (使用切面的方式需要添加)--> 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <!--spring boot jdbc-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

(2)application.yml 设置数据源

#数据库配置
hikari:
  #默认数据库
  -master:
    username: root
    password: aj
    jdbc-url: jdbc:mysql://localhost:3306/task_manage?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    maximum-pool-size: 10
    pool-name: master(localhost)
    connection-timeout: 30000
    idle-timeout: 600000
    max-lifetime: 1765000
    data-source-properties:
      cachePrepStmts: true
      prepStmtCacheSize: 250
      prepStmtCacheSqlLimit: 2048
      useServerPrepStmts: true
      useLocalSessionState: true
      useLocalTransactionState: true
      rewriteBatchedStatements: true
      cacheResultSetMetadata: true
      cacheServerConfiguration: true
      elideSetAutoCommits: true
      maintainTimeStats: false
  # 通用数据库
  -common:
    username: root
    password: aj
    jdbc-url: jdbc:mysql://localhost:3306/task_%s?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

(3)创建线程数据源信息存储类

DynamicDataSourceHolder.java
pac
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值