spring-boot基于yml文件方式整合shrding-jdbc,mybatis-plus实现分库分表和读写分离。

本文详细介绍了如何使用YML文件配置实现数据库的分库分表和读写分离。配置中涉及到mybatis-plus、数据库连接、分片策略以及自定义分表算法等内容,通过具体的配置项展示了如何设置多个数据源、分表规则以及读写分离的主从库配置。
摘要由CSDN通过智能技术生成

直接附上配置

本文主要讲解的是基于yml文件的方式实现分库分表和读写分离的实现。下一遍会讲解基于Config配置文件的方式实现分表分表和读写分离。

yml文件配置

#server
server:
  port: 8081
#mybatis-plus

mybatis-plus:
  typeEnumsPackage: com.modou.entity.enums
  configuration:
    cache-enabled: false
    #      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  global-config:
    db-column-underline: true
    field-strategy: 0
    id-type: 0
    refresh-mapper: true
  mapper-locations: classpath:/mapper/*.xml
  typeAliasesPackage: com.modou.entity
#  logging:
#       level:
#         com.hao.employment.dao: debug
#spring
spring:
  main:
    allow-bean-definition-overriding: true
  profiles:
    include: integration-test
  redis:
    # 0 数据库 切换数据库命令 select 0  *******
    database: 0
    host: 127.0.0.1
    #    password: ************
    port: 6379
  #resources
  resources:
    static-locations: classpath:/public/,classpath:/static/,classpath:/static/static
sharding:
  jdbc:
    dataSource:
      names: db-test0,db-test1
      db-test0: #org.apache.tomcat.jdbc.pool.DataSource
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://*********:3306/modou?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
        password: ******
        maxPoolSize: 20
      db-test1:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://********:3306/modou?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
        password: *******
        maxPoolSize: 20
    props:
      sql:
        show: true
# 第一个需要分表的表名配置     
sharding.jdbc.config.sharding.tables.order_post_addr.actual-data-nodes: ds_0.order_post_addr_$->{0..49}
# 第二个需要分表的表名配置     
sharding.jdbc.config.sharding.tables.order_ext.actual-data-nodes: ds_0.order_ext_$->{0..49}
# 第一个需要分表的分表属性配置     
sharding.jdbc.config.sharding.tables.order_post_addr.table-strategy.standard.sharding-column: sub
# 第二个需要分表的分表属性配置
sharding.jdbc.config.sharding.tables.order_ext.table-strategy.standard.sharding-column: sub
# 分库分表的算法类
sharding.jdbc.config.sharding.tables.order_post_addr.table-strategy.standard.precise-algorithm-class-name: com.modou.config.MyPreciseShardingAlgorithm
# 分库分表的算法类
sharding.jdbc.config.sharding.tables.order_ext.table-strategy.standard.precise-algorithm-class-name: com.modou.config.MyPreciseShardingAlgorithm
# 读写分离配置(主库标识)
sharding.jdbc.config.sharding.master-slave-rules.ds_0.master-data-source-name: db-test0
# 读写分离配置(从库标识)
sharding.jdbc.config.sharding.master-slave-rules.ds_0.slave-data-source-names: db-test1

分表算法类

public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Integer> shardingValue) {
        for (String tableName : availableTargetNames) {
            if (tableName.endsWith(shardingValue.getValue() % 50 + "")) {
                return tableName;
            }
        }
        throw new IllegalArgumentException();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值