nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘xxx‘ not found解决方案

书读的越多而不加思考,你就会觉得你知道得很多;而当你读书而思考得越多的时候,你就会越清楚地看到,你知道得很少。

导读:本篇文章讲解 nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘xxx‘ not found解决方案,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Mybatis 传list 参数出现 nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘xxx’ not found.

一、问题描述:

nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘planSiteList’ not found.

@Insert("<script>INSERT INTO r_plan_site (id, plan_id, site_id, order_number, create_time, status) " +
	"VALUES" + 
	"<foreach collection =\"planSiteList\" item=\"planSite\" index= \"index\" separator =\",\"> " + 
			"(#{id}, #{planId}, #{siteId}, #{orderNumber}, #{createTime}, #{status})" +
	"</foreach>" + 
	"</script>")
	int addPlanSiteDOList( List<PlanSiteDO> planSiteList);
1、解决方案一:

添加注解@Param
在这里插入图片描述

2、解决方案二:

使用Myabtis默认的写法, 修改collection为 list
在这里插入图片描述

3、原因分析

foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

  • 如果传入的是单参数且参数类型是一个List的时候,==collection属性值为list ==.
  • 如果传入的是单参数且参数类型是一个array数组的时候,==collection的属性值为array ==.
  • 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
4、Mybatis默认参数传递方式

Mybatis关于各种类型的单参数默认的写法如下:

类型 接收参数方式
基本数据类型 顺序,如#{0},也可以用name直接获取,如#{name}
List list
数组 array
Map 根据key获取map中各参数即可,如#{key}
自定义的对象 根据get方法对应的参数,使用name获取即可,如#{name}

如果是多参数,比如public User find(String address, List idList), 使用注解@Param(“”)或者考虑封装在map中传递。

二、问题描述:

nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘id’ not found.

在这里插入图片描述

原因分析:

由于item绑定的是planSite对象,因此字段id识别不到,所以将每一个字段改为 planSite.xxx

解决方案:

在这里插入图片描述

相似问题:

捕获全局异常[MyBatisSystemException], msg: [nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘id’ not found. Available parameters are [arg0, startTime, endTime, param3, param1, param2]]

在这里插入图片描述

解决方案:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/122990.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!