sharding-jdbc读写分离快速实现

首先项目是基于springboot框架为基础
1.在pom依赖中导入实现数据库读写分离的sharding依赖

<dependency>
				<groupId>org.apache.shardingsphere</groupId>
				<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
				<version>4.0.0</version>
</dependency>

2.然后在application.yml配置文件中加入数据库连接,其余相关依赖百度一下就可以了

mybatis-plus:
  # 自定义xml文件路径
  mapper-locations: classpath:/mybatis/mapper/**/*.xml
  # 自定义xml文件中用到的实体类路径
  typeAliasesPackage: com.api.springcloud.entities
  configuration:
    # 开启驼峰映射
    map-underscore-to-camel-case: true
    cache-enabled: false
    # 返回map时,true:当查询数据为空时字段返回为null;false:不加这个查询数据为空时,字段将被隐藏
    call-setters-on-nulls: true
    # sql日志打印
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

spring:
  http:
    multipart:
      enabled: true
      max-file-size: 20MB
      max-request-size: 20MB
  application:
    name: microservicecloud-shop
  shardingsphere:
    datasource:
#主数据配置
        master:
            driver-class-name: com.mysql.cj.jdbc.Driver
            type: com.alibaba.druid.pool.DruidDataSource
            url: jdbc:mysql:主数据库连接信息
            username: 主库账号
            password: 主库密码
        names: master,slave
#从数据库配置
        slave:
            driver-class-name: com.mysql.cj.jdbc.Driver
            type: com.alibaba.druid.pool.DruidDataSource
            url: jdbc:从数据库连接信息
            username: 从库账号
            password: 从库密码
    masterslave:
        master-data-source-name: master
        name: ms
        slave-data-source-names: slave
    props:
          sql:
              show: false    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值