错误: Could not set parameters for mapping

时间:2025-03-06 07:42:44

项目场景:

项目场景:Mybatis操作数据库


问题描述

报错

11:46:22,170 ERROR ExceptionFilter:84 -  [DUBBO] Got unchecked and undeclared exception which called by 192.168.200.1. service: , method: findByUsername, exception: : nested exception is : Could not set parameters for mapping: ParameterMapping{property='userId', mode=IN, javaType=class , jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: : Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: :  cannot be cast to , dubbo version: 2.6.0, current host: 127.0.0.1
: nested exception is : Could not set parameters for mapping: ParameterMapping{property='userId', mode=IN, javaType=class , jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: : Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: :  cannot be cast to 
	at (:77)
	at $SqlSessionInterceptor.invoke(:446)
	at .$Proxy37.selectList(Unknown Source)
	at (:230)
	at (:137)
	at (:75)
	at (:59)
	at .$Proxy52.findByUserId(Unknown Source)
	at (:43)
	at $$FastClassBySpringCGLIB$$(<generated>)
	at (:204)

原因分析:

原因:
Integer 不能被映射为 String
cannot be cast to


解决方案:

我一开始以为是在程序中用强制类型转换而出的问题,然而并没有找到哪里用到强制类型转换
再看了看报错原因

Could not set parameters for mapping: ParameterMapping{property='userId', mode=IN, javaType=class , jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: : Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property.

最后查看mapper映射文件,发现我的 parameterType 的类型设置错了

// 接口中的方法
Set<Role> findByUserId(Integer userId);
// mapper映射文件
<select  parameterType="string" resultType="">

将parameterType的值改为 “integer” ,问题解决

总结

Dao接口中的方法参数类型要和mapper映射文件中的 parameterType相对应。