nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping

前言:

在使用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位小伙伴在等你们啦,感兴趣的就赶紧来点击关注我把,哪里有不明白或有不同观点的地方欢迎留言!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开心的码字达

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值