springboot+mybatis中Parameter index out of range (3 > number of parameters, which is 2).的错误

项目场景:

<!--   修改-->
<!-- 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代码写的出了问题:

  1. 检查有没有无关注释写在了sql 语句里面(比如我这个)。
  2. 检查后端传值是不是正确(可以关键方法打断点,一步一步检查传入的值的个数+类型,是不是sql语句需要的参数个数)
  3. 最坑爹的一个就是,别用${id}接java代码中传到xml的id值,建议使用#{id},这个有时候也会导致参数不一致的错误。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值