sharding jdbc 根据id的hash取模分片的配置

核心配置就是Math.abs(id.hashCode())%x,x为要分库或分表的数量。 

spring: 
  shardingsphere: 
    props:
      sql: 
        show: true #控制台打印sql
    datasource: 
      names: ds0,ds1
      ds0: 
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/seal_0?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        username: root
        password: 123456
      ds1: 
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/seal_1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        username: root
        password: 123456
    sharding: 
      tables: 
        user: 
          actual-data-nodes: ds$->{0..1}.user_$->{0..2}
          key-generator: 
            column: id
            type: SNOWFLAKE
            props:
              worker.id: 100
              max.vibration.offset: 2
          database-strategy:
            inline:
              sharding-column: id
              algorithm-expression: ds$->{Math.abs(id.hashCode())%2}
          table-strategy: 
            inline:
              sharding-column: id
              algorithm-expression: user_$->{Math.abs(id.hashCode())%3}  
mybatis:
  config-location: classpath:mybatis-config.xml
  mapper-locations:
  - classpath*:com/zhut/**/sqlmap/*.xml
        

 

你可能感兴趣的:(sharding,jdbc,spring,boot)