Sharding-jdbc 分表不分库注意事项

前言:在根据现在官网的配置老是报错,

就如数据库连接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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值