项目场景:
<!-- 修改-->
<!-- boolean updateCategory(BlogCategory blogCategory);-->
<update id="updateCategory" parameterType="com.atguigu.blog.entity.BlogCategory">
update tb_blog_category
-- <set>
<if test="categoryName != null">
category_name = #{categoryName},
</if>
<if test="categoryIcon != null">
category_icon = #{categoryIcon},
</if>
</set>
where category_id = #{categoryId}
</update>
问题描述:
nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property=‘categoryId’, mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId=‘null’, jdbcTypeName=‘null’, expression=‘null’}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType INTEGER. Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
APP 中接收数据代码:
@Override
public Boolean updateCategory(Integer categoryId, String categoryName, String categoryIcon) {
//封装数据
BlogCategory blogCategory = new BlogCategory();
blogCategory.setCategoryIcon(categoryIcon);
blogCategory.setCategoryName(categoryName);
// blogCategory.setCreateTime(null);
blogCategory.setCategoryId(categoryId);
// blogCategory.setCategoryRank(null);
boolean i = blogCategoryMapper.updateCategory(blogCategory);
return i;
}
原因分析:
这次错误的主要原因是cv大法没使好+编程不需要视力导致的。
仔细看自己的xml文件,才发现set标签前面多了两个–,我特么的,真实瞎了狗眼!!(xml对注释的默认灰色,怕了怕了。)搞了一晚上。
这个注释我刚开始没处理掉,报3>2的错误,试着把参数3换成固定值,结果他报出2>1的参数不匹配的错误。
解决方案:
能出这个问题的原因我百度了下,基本都是xml文件中的sql代码写的出了问题:
- 检查有没有无关注释写在了sql 语句里面(比如我这个)。
- 检查后端传值是不是正确(可以关键方法打断点,一步一步检查传入的值的个数+类型,是不是sql语句需要的参数个数)
- 最坑爹的一个就是,别用${id}接java代码中传到xml的id值,建议使用#{id},这个有时候也会导致参数不一致的错误。