org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘list’ not found报错
使用mybatis时遇到此错误怎么解决。报错原因是:它说下面代码中的
<foreach collection=“list” 这个list没有找到。
<insert id="insertList" parameterType="java.util.List">
INSERT propertyValue(propertyValue_id,propertyValue_value,propertyValue_property_id,propertyValue_product_id)
VALUES
<foreach collection="list" index="index" item="propertyValue" separator=",">
(null,
#{propertyValue.propertyValue_value},
#{propertyValue.propertyValue_property.property_id},
#{propertyValue.propertyValue_product.product_id})
</foreach>
</insert>
但在dao层方法中我们确实是传了一个list集合类型的参数过去
Integer insertList(@Param("propertyValue_list") List<PropertyValue> propertyValueList);
按照mybatis的约定
collection:表示dao层方法传来的参数类型.
如果是数组:collection=“array“
list集合:collection=”list”
set集合:collection=“set”
map集合:collection=“该map集合key的名字”
解决方法是:
1、因为我们对参数起了别名@Param(“propertyValue_list”),所有在sql语句中应该写 collection =”propertyValue_list“
<insert id="insertList" parameterType="java.util.List">
INSERT propertyValue(propertyValue_id,propertyValue_value,propertyValue_property_id,propertyValue_product_id)
VALUES
<foreach collection="propertyValue_list" index="index" item="propertyValue" separator=",">
(null,
#{propertyValue.propertyValue_value},
#{propertyValue.propertyValue_property.property_id},
#{propertyValue.propertyValue_product.product_id})
</foreach>
</insert>
2、或者在dao层的方法中 Integer insertList(List propertyValueList);
去掉@Param(“propertyValue_list”)
测试结果通过: