前言:在根据现在官网的配置老是报错,
就如数据库连接spring.shardingsphere.datasource.m1.url
现在官网是spring.shardingsphere.datasource.m1.jdbc-url
后者始终报错,我也不知道为什么,所以以此记录
1.依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2.配置文件(用的是mysql 8.0)
server.port=10086
# 配置真实数据源
spring.shardingsphere.datasource.names=m1
# 一个实体对应一个数据库多张表必须设置这个
spring.main.allow-bean-definition-overriding=true
# 配置第 1 个数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://192.168.0.76:3306/btb_coffee?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
# 配置表规则
spring.shardingsphere.sharding.tables.demotable.actual-data-nodes=m1.demotable_$->{1..2}
# 指定demotable表里面主键id 生成策略 雪花算法SNOWFLAKE
spring.shardingsphere.sharding.tables.demotable.key-generator.column=id
spring.shardingsphere.sharding.tables.demotable.key-generator.type=SNOWFLAKE
# 指定表分片策略 约定id值偶数添加到demotable_1表,如果cid是奇数添加到demotable_2表
spring.shardingsphere.sharding.tables.demotable.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.demotable.table-strategy.inline.algorithm-expression=demotable_$->{id % 2 + 1}
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true