开发者社区> 问答> 正文

(急)通用mapper3.4,根据主键查询或删除报错,类型转换出错或者不能识别主键?报错

@Liuzh_533 你好,想跟你请教个问题:我最近在升级通用mapper和分页插件时,使用selectByPrimaryKey时报错:

message:nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='createTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Date

我经过调试和查看日志发现sql中的条件是实体类的所有字段:

SELECT  Preparing: SELECT create_time,update_time,description,id,real_name,nick_name,english_name,username,password,address,age,sex FROM rejoice_user WHERE create_time = ? AND update_time = ? AND description = ? AND id = ? AND real_name = ? AND nick_name = ? AND english_name = ? AND username = ? AND password = ? AND address = ? AND age = ? AND sex = ? 
[2017-06-21 10:14:52,415][http-nio-8081-exec-1][ERROR][com.rejoice.active.console.handler.GlobalDefaultExceptionHandler:34] 出错了:nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='createTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Date
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='createTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Date
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) ~[mybatis-spring-1.3.1.jar:1.3.1]

但是我的实体是有加@Id的

 

我的配置集成配置:

我使用的版本:

代码使用:

spring框架是spring boot,但是采用传统方式ImportResource把xml引进来

请问错误产生的原因是什么呢?

查询所有是好的

 

另外我用spring boot的starter集成的时候也有问题:通用mapper不能自动判断主键id的类型:

Preparing: SELECT create_time,update_time,description,id,real_name,nick_name,english_name,username,password,address,age,sex FROM rejoice_user WHERE id = ? 
[2017-06-21 10:12:11,155][http-nio-8081-exec-5][ERROR][com.rejoice.active.console.handler.GlobalDefaultExceptionHandler:34] 出错了:nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='id', mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

这个问题又是怎么产生的呢,根据id删除也是同样情况,查询所有和分页是没问题的

 

展开
收起
爱吃鱼的程序员 2020-06-08 13:29:16 985 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    BaseEntity中是否有id?

    和主键有关的方法中,需要自己保证参数和实体类中的类型一致。

    baseEntity没有id,BaseService中的queryById的的形参Serializable来接收,我升级到3.4后,starter集成的传递的实参跟实体中定义的主键的类型必须一致,而传统的xml配置方式,则出现所有字段当成主键的情况,哪怕是类型跟实体中一致也是一样,有个想法:目前是不支持根据实体类中的主键类型来动态设置参数类型吗,2.0版本支持不(好像版本更新日志有提到主键泛型)
    2020-06-08 13:29:29
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
RowKey与索引设计:技巧与案例分析 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载