目录
一 、 引入pom依赖
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.2</version>
</dependency>
二、 数据源配置
1. 单数据源
spring:
shardingsphere:
datasource:
names: db0
db0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${LOPMARTECH_MYSQL}:3306/dbName?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
2. 多数据源
spring:
shardingsphere:
datasource:
names: db0,db1
db0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${LOPMARTECH_MYSQL}:3306/dbName?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
db1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${LOPMARTECH_MYSQL}:3306/dbName?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
三、分表配置
1、自定义分表策略
spring:
shardingsphere:
datasource:
names: db0
db0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${LOPMARTECH_MYSQL}:3306/dbName?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
rules:
sharding:
tables: #表名
<tableName>:
actual-data-nodes: db0.<tableName>_${'20220709'} #分表后需要使用的表
table-strategy: #分表策略配置
standard:
sharding-column: create_time #分表字段
shardingAlgorithmName: createtime-day #分片算法名称
sharding-algorithms:
createtime-day: #分片算法名称
type: CLASS_BASED # 分片算法配置:自定义类
props: