实现ComplexKeysShardingAlgorithm接口,报错Found different types for sharding value

使用环境:

sharding-jdbc-spring-boot-starter 4.1.1

场景、问题:

实现了ComplexKeysShardingAlgorithm自定义分片接口,单个分表字段类型为Long,SQL中含有between关键字,当值为(i.product_info_id >=123222222222 and i.product_info_id <=898534659142713345)没有问题,当值为(i.product_info_id >=123 and i.product_info_id <=898534659142713345)时报错(由于123小于int的最大值),错误如下:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: org.apache.shardingsphere.underlying.common.exception.ShardingSphereException: Found different types for sharding value Column(name=product_info_id, tableName=product_info).

The error may exist in file [E:\opensource\Sharding-JDBC\sd-jdbc\target\classes\mapper\ProductMapper.xml]

The error may involve com.itheima.shopping.dao.ProductMapper.selectProductList-Inline

The error occurred while setting parameters

SQL: select i.*,d.descript,r.region_name placeOfOrigin, sto.store_name from product_info i join product_descript d on i.product_info_id = d.product_info_id join ps_store_info sto on i.store_info_id = sto.id join region r on i.region_code = r.region_code where (i.product_info_id >=123 and i.product_info_id <=898534659142713345) and i.price BETWEEN 2 and 400 order by product_info_id desc limit ?,?

Cause: org.apache.shardingsphere.underlying.common.exception.ShardingSphereException: Found different types for sharding value Column(name=product_info_id, tableName=product_info).

at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy65.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:166)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:77)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy81.selectProductList(Unknown Source)
at com.itheima.shopping.ShardingTest.testQueryProduct(ShardingTest.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

现状:

现配置如下:


https://gitee.com/haowei0315/sharding-jdbc-demo/tree/master
由于公司加密问题,java文件需要麻烦用.asdf的文件替换内容(可用单元测试类CopyFileTest.testCopt2java还原java文件内容),resource下sql中有对应的脚本,可运行,单元测试类ShardingTest.testQueryProduct方法

这个类型转换的问题应该是 4.x 的 bug,请升级 5.0 试下

京ICP备2021015875号