sharding-jdbc分表配置

一、yml配置

server:
  port: 8080
  tomcat:
    max-swallow-size: -1
  error:
    include-exception: true
    include-stacktrace: ALWAYS
    include-message: ALWAYS
  servlet:
    context-path: /law-boot
    encoding:
      charset: UTF-8
      force: true
      enabled: true
  compression:
    enabled: true
    min-response-size: 1024
    mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*
spring:
  shardingsphere:
    props:
      sql:
        show: true
    datasource:
      names: ds0
      #添加分库数据源
      ds0:
        driverClassName: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.1.xx:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        username: root
        password: xx
        type: com.alibaba.druid.pool.DruidDataSource
    sharding:
      tables:
        t_bzdz:
          actual-data-nodes: ds0.t_bzdz_$->{[610722,610727,610802]}
          table-strategy:
            standard:
              sharding-column: sharding_id
              precise-algorithm-class-name: cn.com.law.sharding.algorithm.BranchPreciseShardingAlgorithm
              #algorithm-expression: t_user_$->{sharding_id % 5}
          ## 生成分布式主键
          key-generator:
            column: f_dzbm
            type: UUID

  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB
  main:
    allow-bean-definition-overriding: true
  jackson:
    date-format: yyyy/MM/dd HH:mm:ss
    time-zone: GMT+8
  #  datasource:
  #    druid:
  #      stat-view-servlet:
  #        enabled: true
  #        loginUsername: admin
  #        loginPassword: 123456
  #        allow:
  #      web-stat-filter:
  #        enable: true
  #    dynamic:
  #      druid:
  #        #设置默认的数据源或数据源组,默认值为master
  #        initialSize: 5
  #        minIdle: 5
  #        maxActive: 30
  #        maxWait: 60000
  #        timeBetweenEvictionRunsMillis: 60000
  #        minEvictableIdleTimeMillis: 300000
  #        validationQuery: SELECT 1
  #        testWhileIdle: true
  #        testOnBorrow: false
  #        testOnReturn: false
  #        poolPreparedStatements: true
  #        maxPoolPreparedStatementPerConnectionSize: 20
  #        filters: stat,wall,slf4j,config
  #        # 允许SELECT语句的WHERE子句是一个永真条件
  #        wall:
  #            selectWhereAlwayTrueCheck: false
  #        useGlobalDataSourceStat: true
  #        stat:
  #          log-slow-sql: true
  #          merge-sql: true
  #          slow-sql-millis: 10000
  #      datasource:
  #        #库名
  #        master:
  #          #此处为数据库链接
  #          url: jdbc:mysql://192.168.1.77:3306/xxxx?characterEncoding=UTF-8&useInformationSchema=true&useUnicode=true&useSSL=false&tinyInt1isBit=false&&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
  #          #数据库驱动
  #          driver-class-name: com.mysql.cj.jdbc.Driver
  #          username: root
  #          password: xxx

#mybatis plus 设置
mybatis-plus:
  map-underscore-to-camel-case: true
  #mapper-locations: classpath*:org/jeecg/modules/**/xml/*Mapper.xml
  global-config:
    # 关闭MP3.0自带的banner
    banner: false
    db-config:
      #主键类型  0:"数据库ID自增",1:"该类型为未设置主键类型", 2:"用户输入ID",3:"全局唯一ID (数字类型唯一ID)", 4:"全局唯一ID UUID",5:"字符串全局唯一ID (idWorker 的字符串表示)";
      id-type: ASSIGN_ID
      # 默认数据库表下划线命名
      table-underline: true
  configuration:
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 返回类型为Map,显示null对应的字段
    call-setters-on-nulls: true
二、pom依赖

<!-- for spring boot 以下三个依赖在开启shardingjdbc时放开-->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>
<!-- 动态数据源 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>4.1.3</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.22</version>
</dependency>

三、分片类

package xx;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 精确分片算法(可用于分库、分表)
 *
 */
public class BranchPreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {
    @Override
    public String doSharding(Collection<String> shardingNameList, PreciseShardingValue<String> preciseShardingValue) {
        String key = String.valueOf(preciseShardingValue.getValue()).substring(0,6);
        //遍历所有的数据分片,与分片键(key)比较,匹配就返回当前数据分片
        for(String shardingName : shardingNameList){
            if(shardingName.contains(key)){
                return shardingName;
            }
        }
        throw new IllegalArgumentException();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值