前言:
在使用mybatis的时候,有的mapper接口需要传入多个类型参数的接口,这时候一不小心就会出现题目中所提到的问题,这篇文章就这种情况出现的bug讲解下bug出现的原因以及解决方案。
正文:
一、复现问题
首先看下我的各层代码:
1.post请求界面
2.controller层代码
3.service层代码
4.serviceimpl层代码
5.dao层代码
6.mapper.xml层代码
错误信息:
nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='age', 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 #2 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
二、分析问题
我从错误信息可以很明显读到类型转换异常的错误,由于又是ibatis报的,那肯定是dao层到 mapper.xml参数注入的时候转换异常啦,可以仔细看下我的参数
List<Student> selectByName(@Param("name") String name, @Param("age") int age,@Param("id")Long id);
有三种数据类型,而我的mapper.xml里参数类型只写了一种 parameterType="java.lang.String",所以参数注入的时候肯定转换异常。
<select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String">
select * from student where name =#{name} and age =#{age} and id=#{id}
</select>
三、解决问题
想解决这个问题,只需要把parameterType去掉即可
<select id="selectByName" resultMap="BaseResultMap">
select * from student where name =#{name} and age =#{age} and id=#{id}
</select>
或者用map和对象进行传参,也可以间接解决,mybatis多参数传参可以见下一篇博客。
总结:
今日份鸡汤,如果不能成为一个有钱的人,那就努力成为一个自己喜欢的人。
我是阿达,一名喜欢分享知识的程序员,时不时的也会荒腔走板的聊一聊电影、电视剧、音乐、漫画,这里已经有7600位小伙伴在等你们啦,感兴趣的就赶紧来点击关注我把,哪里有不明白或有不同观点的地方欢迎留言!