Springboot2.x使用shardingsphere实现分表

话不多说先上pom依赖

    <!-- 分库分表 -->
    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.0.0-RC1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-namespace</artifactId>
        <version>4.0.0-RC1</version>

在实际开发中,如果表的数据过大,我们可能需要把一张表拆分成多张表,这里就是通过ShardingSphere实现分表功能,但不分库

application.properties
# 数据源 saas
spring.shardingsphere.datasource.names=saas

#spring.datasource.url=jdbc:mysql://192.168.2.212:3306/saas?useUnicode=true&characterEncoding=utf8
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 第一个数据库
spring.shardingsphere.datasource.saas.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.saas.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.saas.jdbc-url=jdbc:mysql://192.168.2.212:3306/saas?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&verifyServerCertificate=false&autoReconnct=true&autoReconnectForPools=true&allowMultiQueries=true
spring.shardingsphere.datasource.saas.username=root
spring.shardingsphere.datasource.saas.password=root


#数据分表规则
#指定所需分的表
spring.shardingsphere.sharding.tables.wx_pay_log.actual-data-nodes=saas.wx_pay_log$->{0..5}
#指定主键
spring.shardingsphere.sharding.tables.wx_pay_log.table-strategy.inline.sharding-column=id
#分表规则为主键除以6取模
spring.shardingsphere.sharding.tables.wx_pay_log.table-strategy.inline.algorithm-expression=wx_pay_log$->{id % 6}

# 使用SNOWFLAKE算法生成主键
spring.shardingsphere.sharding.tables.wx_pay_log.key-generator.column = id
spring.shardingsphere.sharding.tables.wx_pay_log.key-generator.type = SNOWFLAKE

#打印sql
spring.shardingsphere.props.sql.show=true

测试

@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/test")
public class test {

    @Autowired
    private PayLogService payLogService;

    @Autowired
    private IdWorker idWorker;
    @RequestMapping(value = "/test1")
    public String test() {

        PayLog payLog = new PayLog();
        for (int i = 0; i < 50; i++) {
            payLog.setId(idWorker.nextId());
            payLog.setBody("666");
            payLog.setLateFee(2);
            payLog.setOrderNum("1231651321651");
            payLogService.insertPayLog(payLog);
        }
        return "执行完毕";
    }
    @RequestMapping(value = "/find")
    public List<PayLog> findAll(){
        List<PayLog> allPayLog = payLogService.findAllPayLog();
        return allPayLog;
    }

}

Mysql 我用了6张表

每张表的数据都会不一样

总结

遇到不能分表的情况,只有一个原因.就是配置文件的问题.仔细检查检查.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值