项目场景:

项目场景:Mybatis操作数据库


问题描述

报错

11:46:22,170 ERROR ExceptionFilter:84 -  [DUBBO] Got unchecked and undeclared exception which called by 192.168.200.1. service: com.chenjiayan.service.UserService, method: findByUsername, exception: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='userId', mode=IN, javaType=class java.lang.String, 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.lang.String, dubbo version: 2.6.0, current host: 127.0.0.1
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='userId', mode=IN, javaType=class java.lang.String, 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.lang.String
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
	at com.sun.proxy.$Proxy37.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
	at com.sun.proxy.$Proxy52.findByUserId(Unknown Source)
	at com.chenjiayan.service.impl.UserServiceImpl.findByUsername(UserServiceImpl.java:43)
	at com.chenjiayan.service.impl.UserServiceImpl$$FastClassBySpringCGLIB$$8c804ae4.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

原因分析:

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


解决方案:

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

Could not set parameters for mapping: ParameterMapping{property='userId', mode=IN, javaType=class java.lang.String, 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.

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

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

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

总结

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

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐