前言:

在使用mybatis的时候,有的mapper接口需要传入多个类型参数的接口,这时候一不小心就会出现题目中所提到的问题,这篇文章就这种情况出现的bug讲解下bug出现的原因以及解决方案。

正文:

一、复现问题

首先看下我的各层代码:

1.post请求界面

nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping_开发问题

2.controller层代码

nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping_开发问题_02

 3.service层代码

nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping_开发问题_03

4.serviceimpl层代码

nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping_开发问题_04

5.dao层代码

nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping_开发问题_05

6.mapper.xml层代码

nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping_开发问题_06

错误信息:

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多参数传参可以见下一篇博客。

总结:

今日份鸡汤,如果不能成为一个有钱的人,那就努力成为一个自己喜欢的人。