【Java报错】mapper传入array\collection\list类型的参数时报BindingException:Parameter `` not found(问题复现+3种解决方法)
1. 问题说明
这里只贴出核心代码:
// 查询数据
List<StatisticalInfo> statisticalInfoList = null;
// 保存数据
boolean isSuccess = baseComponent.batchInsert(statisticalInfoList);
这里是批量保存数据时使用的SQL:
<insert id="batchInsert" parameterType="com.xxx.StatisticalInfo">insert into statistical_info (id, table_name, group_time, total_rows)values<foreach collection="statisticalInfoList" index="index" item="item" separator=",">( #{item.id}, #{item.tableName}, #{item.groupTime}, #{item.totalRows} )</foreach>
</insert>
报错信息如下:
nested exception is org.apache.ibatis.binding.BindingException:
Parameter 'statisticalInfoList' not found. Available parameters are [collection, list]
2. 解决
【方法 1️⃣】只将【collection=“statisticalInfoList”】修改为【collection=“list”】即可(不用修改其他任何文件):
<insert id="batchInsert" parameterType="com.xxx.StatisticalInfo">insert into statistical_info (id, table_name, group_time, total_rows)values<foreach collection="list" index="index" item="item" separator=",">( #{item.id}, #{item.tableName}, #{item.groupTime}, #{item.totalRows} )</foreach>
</insert>
【方法 2️⃣】只在Mapper接口给参数添加 @Param(“statisticalInfoList”) 注解(不用修改mapper文件):
public interface BaseMapper {/*** 批量导入统计结果数据** @param statisticalInfoList 查询的统计结果* @return 导入成功标志*/boolean batchInsert(@Param("statisticalInfoList") List<StatisticalInfo> statisticalInfoList);
}
mapper文件里的SQL依然是【collection=“statisticalInfoList”】:
<insert id="batchInsert" parameterType="com.xxx.StatisticalInfo">insert into statistical_info (id, table_name, group_time, total_rows)values<foreach collection="statisticalInfoList" index="index" item="item" separator=",">( #{item.id}, #{item.tableName}, #{item.groupTime}, #{item.totalRows} )</foreach>
</insert>
【方法 3️⃣】不修改接口,将参数封装成map对象(适用于多个参数情况)这里添加了一个 schemaName 参数:
// 将参数封装
HashMap<String, Object> parameter = new HashMap<>(2);
parameter.put("insertList", statisticalInfoList);
parameter.put("schemaName", schemaName);
mapper文件里的SQL参数类型修改【parameterType=“map”】collection值修改成map对应的key值【collection=“insertList”】:
<insert id="batchInsertStatisticalInfo" parameterType="map">insert into ${schemaName}stat_data_source (id, table_name, group_time, total_rows)values<foreach collection="insertList" index="index" item="item" separator=",">( #{item.id}, #{item.tableName}, #{item.groupTime}, #{item.totalRows} )</foreach>
</insert>
❌ 注意: 如果你传入的参数是List对象,而collection写的却是array【collection=“array”】
<insert id="batchInsert" parameterType="com.xxx.StatisticalInfo">insert into statistical_info (id, table_name, group_time, total_rows)values<foreach collection="array" index="index" item="item" separator=",">( #{item.id}, #{item.tableName}, #{item.groupTime}, #{item.totalRows} )</foreach>
</insert>
将会报如下错误:
nested exception is org.apache.ibatis.binding.BindingException:
Parameter 'array' not found. Available parameters are [collection, list]
使用时要特别注意
【Java报错】mapper传入array\collection\list类型的参数时报BindingException:Parameter `` not found(问题复现+3种解决方法)相关推荐
- manjaro软件源报错 不停看到错误 “PackageName: signature from “User <email@archlinux.org>“ is invalid“ 的几种解决方法
manjaro软件源报错 不停看到错误 "PackageName: signature from "User " is invalid" 的几种解决方法 参考文 ...
- C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法
C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法 参考文章: (1)C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文 ...
- 企业微信登录报错:应用程序无法正常启动(0xc0000142);Win10应用程序无法正常启动0xc0000142错误的解决方法
企业微信登录报错,有时候要点击多次才能登陆. 解决办法:创建一个文本,把下面一段复制进去,文本后缀改成.bat,启动运行一下即可. sc stop NSFFileCtl sc config NSFFi ...
- 前端报错如何在服务器中显示,详解Vue项目中出现Loading chunk {n} failed问题的解决方法...
最近有个Vue项目中会偶尔出现Loading chunk {n} failed的报错,报错来自于webpack进行code spilt之后某些bundle文件lazy loading失败.但是这个问题 ...
- mybatis报错:Result Maps collection already contains value for model.dao.UserMapper.BaseResultMap
数据库:8.0.11 数据库驱动包为:mysql-connector-java-8.0.11.jar 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件后,把文件 ...
- java报错MalformedURLException: unknown protocol: c
java报错:MalformedURLException: unknown protocol: c 1. 报错情况: 部分代码: //打开图片path="C:/Users/MyUser/im ...
- java报错-找不到或无法加载主类(Error: Could not find or load main class)
此文首发于我的个人博客:java报错-找不到或无法加载主类(Error Could not find or load main class) - zhang0peter的个人博客 比如说test.ja ...
- 解决MongoDB报错:Cannot create collection “None“ as a capped collection as it already exist
解决MongoDB报错:Cannot create collection "None" as a capped collection as it already exist Roo ...
- 【Java报错找不到指定文件】Exception in thread “main“ java.io.FileNotFoundException:...... (系统找不到指定的文件。)
出错代码 (这段代码位于Src_exp2_3.java中) public static String getValue(String key) throws IOException{Propertie ...
最新文章
- opencv_contrib编译失败解决方法
- 如何对单手和双手协同运动方向进行神经表征和解码?北理工研究团队给出了相关方案
- 数据结构源码笔记(C语言):Huffman树字符编码
- 缺省的servlet(了解)
- idea 2020版的Default Setting
- VS2010 C++ 插件 VissualAssistX 安装
- java中identifiers什么意思_javassist.是什么意思
- python小说阅读器_用python实现自己的小说阅读器
- Ubuntu MySQL 重新安装
- 组合模式Composite
- srgan要训练多久_儿童内裤多久换一次,穿多久要扔掉?
- python填空题及答案知乎_zhihu-python
- HTML | 分享几个HTML邮件样式模板
- 创作焦虑之下,红人大V怎么看微博?
- 荣之学教育汇总Shopee平台最全基础知识
- esp32 红外接收
- 游戏开发工具引擎/模拟器收集
- 鸟哥的linux私房菜学习笔记7
- GitHub的镜像登陆显示Whoa there!解决办法
- 数字图像处理(1)——认识数字图像
热门文章
- Wdcp在安装memcached出现错误的解决办法
- windows常用命令行整理
- 炸弹人游戏开发系列(6):实现碰撞检测,设置移动步长
- poj 1469 二分图最大匹配
- Microsoft Speech SDK 5.1
- python中线条颜色_python中plot用法——线条、点、颜色
- yii2 mysql between_yii2:多条件多where条件下碰到between时,between语句如何处理呢?
- shell-sort
- java groovy 动态计算_计算Java / Groovy中的经过时间
- Java格式化日期和时间模式占位符