解决错误:nested exception is org.apache.ibatis.binding.BindingException

mybatis报错信息:

Error: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘categoryList’ not found. Available parameters are [arg0, collection, list]

网上搜到的解决办法:
一、多个参数使用@Param注解标识
对于多个参数的情况,mapper.java中用注解标识参数, mapper.xml中才能识别到其值
正确示例:
mapper.java

void save(@Param("userId")Integer userId, @Param("roleId")Integer roleId);

mapper.xml

INSERT INTO t_user (user_id, role_id) VALUES (#{userId}, #{roleId})

不管用。
我的错误代码:

int saveBatch(List<BlogCategory> categoryList);
    <insert id="saveBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO blog_category (category_name, create_time, create_by)
        VALUES
        <foreach collection="categoryList" item="category" separator=",">
            (#{category.categoryName}, #{category.createTime}, #{category.createBy})
        </foreach>
    </insert>

报错:

nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘categoryList’ not found. Available parameters are [arg0, collection, list]

尝试加入@Param 依然没用,
最后发现把xml中 collection=“categoryList” 改为 collection=“list” 不报错了

正确代码:

int saveBatch(List<BlogCategory> categoryList);
//或者
int saveBatch(@Pararm("list") List<BlogCategory> categoryList);
    <insert id="saveBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO blog_category (category_name, create_time, create_by)
        VALUES
        <foreach collection="list" item="category" separator=",">
            (#{category.categoryName}, #{category.createTime}, #{category.createBy})
        </foreach>
    </insert>

查找原因:
MyBatis 参数解析机制
MyBatis 时,需要将方法参数映射到 SQL 语句中。当方法参数是一个简单类型(如 int, String)或一个对象时,MyBatis 可以直接使用参数。然而,当参数是一个集合(如 List 或 Map)时,MyBatis 的处理方式就不同了。

对于单个集合参数(如一个 List),MyBatis 默认将其视为 “list”。这是因为在底层,MyBatis 将这种类型的参数封装在一个 Map 中,其中:
对于单个非集合参数,MyBatis 使用 “param1”, “param2”, … 作为键。
对于集合参数,MyBatis 使用 “collection”(对于任意集合类型)和 “list”(对于 List 类型)作为默认键。
如果使用 @Param 注解指定了名称,MyBatis 将使用你提供的名称。

总结
使用 collection=“list” 问题解决了。
但是当我尝试使用@Param 指定名称,并在MyBatis 中使用时,仍然报错。不知道为什么,希望有大佬看到能解答一下。
报错代码:

int saveBatch(@Param("categoryList") List<BlogCategory> categoryList);
    <insert id="saveBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO blog_category (category_name, create_time, create_by)
        VALUES
        <foreach collection="categoryList" item="category" separator=",">
            (#{category.categoryName}, #{category.createTime}, #{category.createBy})
        </foreach>
    </insert>

错误信息:

Error: nested exception is org.apache.ibatis.binding.BindingException:Parameter ‘categoryList’ not found. Available parameters are [arg0, collection, list]

相关推荐

  1. Composer常见错误解决

    2024-02-21 19:12:02       37 阅读
  2. Composer常见错误解决

    2024-02-21 19:12:02       42 阅读
  3. Composer常见错误解决

    2024-02-21 19:12:02       62 阅读
  4. Composer常见错误解决

    2024-02-21 19:12:02       58 阅读
  5. Composer 常见错误解决

    2024-02-21 19:12:02       32 阅读
  6. composer常见错误解决

    2024-02-21 19:12:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 19:12:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 19:12:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 19:12:02       87 阅读
  4. Python语言-面向对象

    2024-02-21 19:12:02       96 阅读

热门阅读

  1. 飞常准查航班小程序采集

    2024-02-21 19:12:02       51 阅读
  2. SpringBoot+WebSocket实现即时通讯(三)

    2024-02-21 19:12:02       49 阅读
  3. Android引入aar包的方法

    2024-02-21 19:12:02       49 阅读
  4. QT6不同场景下的一些代码

    2024-02-21 19:12:02       50 阅读
  5. 接口(一)

    2024-02-21 19:12:02       51 阅读
  6. feign远程调用请求头设置参数

    2024-02-21 19:12:02       50 阅读
  7. QT day1

    QT day1

    2024-02-21 19:12:02      53 阅读
  8. 下载图片到本地,多个图片压缩后下载到本地

    2024-02-21 19:12:02       50 阅读