使用shardingsphere4.1.1跨表分页查询时分页参数解析错误

问题描述

Shardingsphere由4.0.1切换到4.1.1版本后,发现在跨表(即查询涉及多个表)分页查询的时候查不出数据。通过查看打印的sql发现设置的参数很是奇怪,如下图所示:

lq490t.png

问题原因

经过漫长的调试终于发现了问题所在,一切言语尽在图中:

bbm5ai.png

09bl4h.png

f0d022.png

解决方案

通过问题分析我们知道问题点就是shardingsphere4.1.1版本在解析查询条件时不会记录非数据库字段的参数,我们需要根据项目的实际情况去找适合的解决方案。笔者这边的解决方案是将#替换为$,在sharding拿到sql前就已经进行了变量替换,从而避免参与到sharding的参数解析中。效果如图:

1a0930.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的使用ShardingSphere实现分库分表的Java例子: 首先,需要在pom.xml文件中添加ShardingSphere的依赖: ``` <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>4.1.1</version> </dependency> ``` 接着,需要在Spring配置文件中添加ShardingSphere的数据源配置: ``` spring.shardingsphere.datasource.names=ds0, ds1 spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/db0?useUnicode=true&characterEncoding=utf-8 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password=root spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8 spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=root spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds${0..1}.user_${0..1} spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=user_id spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.example.algorithm.ModuloShardingTableAlgorithm spring.shardingsphere.sharding.tables.user.table-strategy.standard.range-algorithm-class-name=com.example.algorithm.RangeShardingTableAlgorithm ``` 注意,上面的例子中使用了两个数据源ds0和ds1,分别对应了两个数据库db0和db1。同使用ShardingSphere的分片表策略,将user表按照user_id字段进行分片,使用了ModuloShardingTableAlgorithm和RangeShardingTableAlgorithm算法来实现分片。 最后,需要实现两个算法类: ModuloShardingTableAlgorithm.java: ``` public final class ModuloShardingTableAlgorithm implements PreciseShardingAlgorithm<Integer> { @Override public String doSharding(Collection<String> tableNames, PreciseShardingValue<Integer> shardingValue) { for (String each : tableNames) { if (each.endsWith(shardingValue.getValue() % tableNames.size() + "")) { return each; } } throw new IllegalArgumentException(); } } ``` RangeShardingTableAlgorithm.java: ``` public final class RangeShardingTableAlgorithm implements RangeShardingAlgorithm<Integer> { @Override public Collection<String> doSharding(Collection<String> tableNames, RangeShardingValue<Integer> shardingValue) { Collection<String> result = new LinkedHashSet<>(); for (Integer i = shardingValue.getValueRange().lowerEndpoint(); i <= shardingValue.getValueRange().upperEndpoint(); i++) { for (String each : tableNames) { if (each.endsWith(i % tableNames.size() + "")) { result.add(each); } } } return result; } } ``` 这样,就可以使用ShardingSphere实现分库分表了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值