假如有 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
1
lalawu 2023-08-17 17:51:12 +08:00
介意贴完整的报错堆栈吗
|
2
qinfengge OP @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) ``` |
3
wdlth 2023-08-17 20:39:53 +08:00
可以自定义一个实现 RangeShardingAlgorithm 接口的范围分片策略
|
4
qinfengge OP 看到了这个
https://blog.csdn.net/w_t_y_y/article/details/121997911?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-121997911-blog-106113983.235^v38^pc_relevant_default_base&spm=1001.2101.3001.4242.1&utm_relevant_index=3 |
5
lalawu 2023-08-18 09:48:39 +08:00
楼上已答,楼主你也自己找到了答案,恭喜。完整的堆栈会详细说明酒精是哪里执行有问题,比如上面堆栈表明没有对应的分片策略 ava.lang.UnsupportedOperationException: Cannot find range sharding strategy in sharding rule.
|