zoukankan      html  css  js  c++  java
  • Error updating database. Cause: java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object;

    在用Mybatis时遇到了一个让我折腾了一晚上的问题(好歹接触Mybatis半年多了,自我感觉不应该存在什么致命盲区了),谁知道这个问题直接把人给干傻了。

    先描述问题情况:当时正在写一个接收json数组(用户的ID),来批量删除用户的代码。我将数据封装到了int数组中(因为只是一个很简单的内部管理系统,就直接用整型做的ID)。mapper内容如下:

    <!--逻辑删除用户-->
    <update id="deleteByIds">
        update staff set deleted = 1
        where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    

    写完以后自然要测试,一测试就报了下面的异常:

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
    ### Error updating database.  Cause: java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object; ([I and [Ljava.lang.Object; are in module java.base of loader 'bootstrap')
    ### The error may exist in file [F:geek-and-hard-workingbee-member-management	argetclassesmappersStaffMapper.xml]
    ### The error may involve com.geekbee.membermanagement.mapper.StaffMapper.deleteByIds
    ### The error occurred while executing an update
    ### Cause: java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object; ([I and [Ljava.lang.Object; are in module java.base of loader 'bootstrap')
    

    从报错上看,说是I(如果了解过Java虚拟机应该知道在虚拟机内部是用I来表示int的)不能转化为Object(前面的L表示这是引用类型),其实这个报错是很明确的,但我不信邪啊。开了Debug,打算看看为会这样(为什么Mybatis可以接受int做参数,却不能接收int数组)。
    结果Debug显示数组内容是正常的,但是Mybatis的方法内部调用压根没法看,反复试了几次,受不了了,就把int数组改成了String数组,一测试,OK了。
    直接说结论,Mybatis似乎是不能接受基本数据类型数组的,保险起见可以直接用String数组,当然也可以用包装类数组试试。

  • 相关阅读:
    解决RobotFramework的关键字不能高亮的问题
    使用Python遇到:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 问题
    通过Jekins执行bat脚本始终无法完成
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
    [转]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    HDU 2686 MCMF
    HDU 4278 卡特兰,区间DP
    POJ 2985 名次树
    POJ 2531 深搜剪枝
    Uva 10061 进制问题
  • 原文地址:https://www.cnblogs.com/simplelong/p/14775481.html
Copyright © 2011-2022 走看看