Springboot整合baomidou进行读写分离

pom依赖


<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.6.1</version>
</dependency>

主从配置


package com.baomidou.config;


import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin;
import com.baomidou.interceptor.MasterSlaveAutoRoutingOrSelectByRulePlugin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 主从配置
 */
@Configuration
public class MasterSlaveAutoRoutingPluginConfig {

    @Bean
    public MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin(){
        return new MasterSlaveAutoRoutingPlugin();
    }

}

yml


server:
  port: 9914
  servlet:
    context-path: /baomidou
logging:
  level:
    com.baomidou.dynamic: debug
spring:
  datasource:
    dynamic:
      primary: master  # 这里需要修改
      strict: true
      hikari:
        pool-name: Yi_HikariCP
        minimum-idle: 5
        maximum-pool-size: 20
      datasource:
        # 测试
        master:
          type: com.zaxxer.hikari.HikariDataSource
          url: jdbc:mysql://localhost:3396/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
          username: admin
          password: ufcz2b8x3bas4c5m$%332
          driver-class-name: com.mysql.cj.jdbc.Driver
        slave_1:
          type: com.zaxxer.hikari.HikariDataSource
          url: jdbc:mysql://localhost:3397/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
          username: readonly
          password: 123qwe!@#
          driver-class-name: com.mysql.cj.jdbc.Driver
        slave_2:
          type: com.zaxxer.hikari.HikariDataSource
          url: jdbc:mysql://localhost:3397/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
          username: readonly
          password: 123qwe!@#
          driver-class-name: com.mysql.cj.jdbc.Driver
      #seata事务生效
      seata: true
      seata-mode: at

seata:
  enabled: true
  application-id: baomidou-seata
  #不使用自动代理数据源
  enable-auto-data-source-proxy: false
#================================================= mybatis-plus start =================================================
mybatis-plus:
  # classpath:/mapper/*Mapper.xml
  mapper-locations: classpath*:/mapper/*.xml
  # 实体扫描,多个package用逗号或者分号分隔
  type-aliases-package: com.user.entity
  configuration:
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 驼峰下划线转换
    map-underscore-to-camel-case: true
    cache-enabled: false
    # 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
    call-setters-on-nulls: false
  global-config:
    # 刷新mapper 调试神器
    refresh: true
    banner: false
    #数据库大写下划线转换
    #capital-mode: true
    #序列接口实现类配置
    #key-generator: com.baomidou.springboot.xxx
    # 数据库相关配置
    db-config:
      db-type: mysql
      # 主键类型  AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
      id-type: UUID
      # 字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
      field-strategy: NOT_EMPTY
      capital-mode: true
      #逻辑删除配置
      logic-delete-value: 1
      logic-not-delete-value: 0
#================================================= mybatis-plus end ===================================================

使用方式


根据@DS 注解切换数据源
@DS("master")@DS("slave")
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值