V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
qinfengge
V2EX  ›  Java

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

  •  
  •   qinfengge · 2023-08-17 17:45:43 +08:00 · 1343 次点击
    这是一个创建于 464 天前的主题,其中的信息可能已经有所发展或是发生改变。

    假如有 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

    6 条回复    2023-08-18 10:27:18 +08:00
    lalawu
        1
    lalawu  
       2023-08-17 17:51:12 +08:00
    介意贴完整的报错堆栈吗
    qinfengge
        2
    qinfengge  
    OP
       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
        3
    wdlth  
       2023-08-17 20:39:53 +08:00
    可以自定义一个实现 RangeShardingAlgorithm 接口的范围分片策略
    qinfengge
        4
    qinfengge  
    OP
       2023-08-18 09:09:36 +08:00
    lalawu
        5
    lalawu  
       2023-08-18 09:48:39 +08:00
    楼上已答,楼主你也自己找到了答案,恭喜。完整的堆栈会详细说明酒精是哪里执行有问题,比如上面堆栈表明没有对应的分片策略 ava.lang.UnsupportedOperationException: Cannot find range sharding strategy in sharding rule.
    qinfengge
        6
    qinfengge  
    OP
       2023-08-18 10:27:18 +08:00
    @lalawu #5 感谢大佬回答,之前粗心了没看到这个日志🤔
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2698 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 10:16 · PVG 18:16 · LAX 02:16 · JFK 05:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.