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

本文主要是介绍解决错误: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]

这篇关于解决错误:nested exception is org.apache.ibatis.binding.BindingException的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/731557

相关文章

解决idea启动项目报错java: OutOfMemoryError: insufficient memory

《解决idea启动项目报错java:OutOfMemoryError:insufficientmemory》:本文主要介绍解决idea启动项目报错java:OutOfMemoryError... 目录原因:解决:总结 原因:在Java中遇到OutOfMemoryError: insufficient me

maven异常Invalid bound statement(not found)的问题解决

《maven异常Invalidboundstatement(notfound)的问题解决》本文详细介绍了Maven项目中常见的Invalidboundstatement异常及其解决方案,文中通过... 目录Maven异常:Invalid bound statement (not found) 详解问题描述可

SpringBoot项目整合Netty启动失败的常见错误总结

《SpringBoot项目整合Netty启动失败的常见错误总结》本文总结了SpringBoot集成Netty时常见的8类问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、端口冲突问题1. Tomcat与Netty端口冲突二、主线程被阻塞问题1. Netty启动阻

SpringBoot整合Kafka启动失败的常见错误问题总结(推荐)

《SpringBoot整合Kafka启动失败的常见错误问题总结(推荐)》本文总结了SpringBoot项目整合Kafka启动失败的常见错误,包括Kafka服务器连接问题、序列化配置错误、依赖配置问题、... 目录一、Kafka服务器连接问题1. Kafka服务器无法连接2. 开发环境与生产环境网络不通二、序

SpringBoot整合Apache Spark实现一个简单的数据分析功能

《SpringBoot整合ApacheSpark实现一个简单的数据分析功能》ApacheSpark是一个开源的大数据处理框架,它提供了丰富的功能和API,用于分布式数据处理、数据分析和机器学习等任务... 目录第一步、添加android依赖第二步、编写配置类第三步、编写控制类启动项目并测试总结ApacheS

nacos服务无法注册到nacos服务中心问题及解决

《nacos服务无法注册到nacos服务中心问题及解决》本文详细描述了在Linux服务器上使用Tomcat启动Java程序时,服务无法注册到Nacos的排查过程,通过一系列排查步骤,发现问题出在Tom... 目录简介依赖异常情况排查断点调试原因解决NacosRegisterOnWar结果总结简介1、程序在

Java Exception异常类的继承体系详解

《JavaException异常类的继承体系详解》Java中的异常处理机制分为异常(Exception)和错误(Error)两大类,异常分为编译时异常(CheckedException)和运行时异常... 目录1. 异常类的继承体系2. Error错误3. Exception异常3.1 编译时异常: Che

Java Exception与RuntimeException使用及说明

《JavaException与RuntimeException使用及说明》:本文主要介绍JavaException与RuntimeException使用及说明,具有很好的参考价值,希望对大家有所... 目录简介ExceptionRuntimeException自定义异常选择继承Exception(受检异常)

解决java.util.RandomAccessSubList cannot be cast to java.util.ArrayList错误的问题

《解决java.util.RandomAccessSubListcannotbecasttojava.util.ArrayList错误的问题》当你尝试将RandomAccessSubList... 目录Java.util.RandomAccessSubList cannot be cast to java.

Apache服务器IP自动跳转域名的问题及解决方案

《Apache服务器IP自动跳转域名的问题及解决方案》本教程将详细介绍如何通过Apache虚拟主机配置实现这一功能,并解决常见问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录​​问题背景​​解决方案​​方法 1:修改 httpd-vhosts.conf(推荐)​​步骤