关于 shareding jdbc 是否支持 mybatis-plus 的 ge.le,gt.lt 比较

2023-08-17 17:45:43 +08:00
 qinfengge

假如有 2 张表水平分表,且分表算法如下

public class ResultPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {
        try {
            Integer id = preciseShardingValue.getValue();
            if (id < 8000000) {
                return "inspection_result";
            } else {
                return "inspection_result_home";
            }
        } catch (NumberFormatException e) {
            log.error("在分表时 id 转换异常");
            throw new RuntimeException(e);
        }
    }
}

在我想查询某张表中的数据时,首先想到的就是根据 id 比较

public Result<Object> testSj(){
        LambdaQueryWrapper<InspectionResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(InspectionResult::getId, 8000000);
        return Result.ok(inspectionResultMapper.selectList(wrapper));
    }

但是它报错了

Error querying database.  Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: Method queryTotal execution error of sql :
 SELECT COUNT(1) FROM inspection_result WHERE (id >= ?)

搜了很多都没找到解释,有没有大佬解释下原因? shareding jdbc 版本 4.1.1

1348 次点击
所在节点    Java
6 条回复
lalawu
2023-08-17 17:51:12 +08:00
介意贴完整的报错堆栈吗
qinfengge
2023-08-17 17:56:03 +08:00
@lalawu 当然可以

```java
023-08-17 17:55:23,484 - 请求 /signTest/testSj 出错,请求参数{},请求地址 127.0.0.1 ,错误信息:
### Error querying database. Cause: java.lang.UnsupportedOperationException: Cannot find range sharding strategy in sharding rule.
### The error may exist in com/jkkj/mapper/user/InspectionResultMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT * FROM inspection_result WHERE (id >= ?)
### Cause: java.lang.UnsupportedOperationException: Cannot find range sharding strategy in sharding rule.
2023-08-17 17:55:23,571 - Result(code=201, message=失败, timestamp=1692266123571, data=请求 /signTest/testSj 出错,请求参数{},错误信息:nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.UnsupportedOperationException: Cannot find range sharding strategy in sharding rule.
### The error may exist in com/jkkj/mapper/user/InspectionResultMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT * FROM inspection_result WHERE (id >= ?)
### Cause: java.lang.UnsupportedOperationException: Cannot find range sharding strategy in sharding rule., ok=null)
```
wdlth
2023-08-17 20:39:53 +08:00
可以自定义一个实现 RangeShardingAlgorithm 接口的范围分片策略
qinfengge
2023-08-18 09:09:36 +08:00
lalawu
2023-08-18 09:48:39 +08:00
楼上已答,楼主你也自己找到了答案,恭喜。完整的堆栈会详细说明酒精是哪里执行有问题,比如上面堆栈表明没有对应的分片策略 ava.lang.UnsupportedOperationException: Cannot find range sharding strategy in sharding rule.
qinfengge
2023-08-18 10:27:18 +08:00
@lalawu #5 感谢大佬回答,之前粗心了没看到这个日志🤔

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/966168

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX