springboot2+mybatis+shardingsphere-5.5.1
注意:
1.druid不能boot-starter方式引入
2.snakeyaml需要1.33 ('void org.yaml.snakeyaml.LoaderOptions.setCodePointLimit(int)') #30318
3.spring.datasource.driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver
4.如果使用了quartz,需要指定独立数据源(Table or view 'QRTZ_LOCKS' does not exist.)
5.druid的密码加密正常
6.druid的配置需放在 shardingsphere的yaml文件中,且为驼峰
以下是具体代码,主实现用户日志按月存表
1.pom
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc</artifactId> </dependency> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.33</version> </dependency>
2.application.yml
spring:
datasource:
driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver
url: jdbc:shardingsphere:classpath:sharding-dev.yml
3.sharding-dev.yml
mode:
type: Standalone
repository:
type: JDBC
dataSources:
ds0:
dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?useSSL=false&createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&noAccessToProcedureBodies=true&rewriteBatchedStatements=true
username: root
# password: root
password: root
initialSize: 5
minIdle: 10
maxActive: 50
maxWait: 120000
asyncInit: true
timeBetweenEvictionRunsMillis: 120000
minEvictableIdleTimeMillis: 60000
maxEvictableIdleTimeMillis: 600000
validationQuery: select 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
connectTimeout: 120000
socketTimeout: 120000
filters: wall,config
#密码加密
connectionProperties: config.decrypt=true;config.decrypt.key=publicKey
rules: # 配置 表规则
- !SHARDING
tables:
user_operation_log: # 分表,表名
# 配置分库策略
# 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
actualDataNodes: ds0.user_operation_log_$->{202411..202412} #数据节点,均匀分布
tableStrategy: # 配置分表策略
standard:
shardingColumn: create_time
shardingAlgorithmName: table-inline
# 配置 分片算法
shardingAlgorithms:
table-inline:
type: INLINE
props:
algorithm-expression: user_operation_log_$->{create_time.toString().substring(0, 7).replace("-", "")} #按模运算分配
allow-range-query-with-inline-sharding: true
- !SINGLE
tables:
# MySQL 风格
- ds0.* # 加载指定数据源中的全部单表
defaultDataSource: ds0 # 默认数据源,仅在执行 CREATE TABLE 创建单表时有效。缺失值为空,表示随机单播路由。
props:
sql-show: true

浙公网安备 33010602011771号