sharding-jdbc springboot 配置数据水平分表

1.增加pom坐标

 <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>

2.增加配置文件

#数据源
spring.shardingsphere.datasource.names=master0
#配置数据源
spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://192.168.101.24:3306/anyouliang_exchange
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=123456

#spring.shardingsphere.datasource.master0slave0.type=com.alibaba.druid.pool.DruidDataSource
#spring.shardingsphere.datasource.master0slave0.driver-class-name=com.mysql.jdbc.Driver
#spring.shardingsphere.datasource.master0slave0.url=jdbc:mysql://192.168.101.47:3306/anyouliang_exchange
#spring.shardingsphere.datasource.master0slave0.username=root
#spring.shardingsphere.datasource.master0slave0.password=123456

#配置指定表的数据节点
spring.shardingsphere.sharding.tables.vouchers_of_sale.actual-data-nodes=master0.vouchers_of_sale_$->{1..2}

#主键生成策略
spring.shardingsphere.sharding.tables.vouchers_of_sale.key-generator.column=sale_id
spring.shardingsphere.sharding.tables.vouchers_of_sale.key-generator.type=SNOWFLAKE

#指定vouchers_of_sale表的分片策略,包括分片建表和分片算法
spring.shardingsphere.sharding.tables.vouchers_of_sale.table-strategy.inline.sharding-column=sale_id
spring.shardingsphere.sharding.tables.vouchers_of_sale.table-strategy.inline.algorithm-expression=vouchers_of_sale_$->{sale_id % 2+1}

spring.shardingsphere.props.sql.show = true

3.项目中如果有多个数据源,删除其他数据源。orm可以用jpa mybatis等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值