猿问

JAVA MyBatis批量操作(插入)时异常

传入为 List<Object> 确定为不为空


org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '__frch_item_0' not found.


<insert id="batchInsert">

    insert into

    personal_tag(type, tag, create_time, open_account_id)

    values

    <foreach collection="list" item="item" separator=",">

        (#{item.type,jdbcType=INTEGER},

        #{item.tag,jdbcType=VARCHAR},

        #{item.createTme,jdbcType=TIMESTAMP},

        #{item.openAccountId,jdbcType=BIGINT})

    </foreach>ON DUPLICATE KEY UPDATE tag=tag

</insert>

代码如上....


确定传入值不为空


忘记补充,我的项目里其他地方也有批量插入的代码 , 类似于这个 全部都是正确执行的

mybatis ver : 3.3.0


江户川乱折腾
浏览 2121回答 5
5回答

梦里花落0921

此问 关闭 题主没有找到任何答案 靠升级mybatis版本后解决此问题

繁星coding

在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。<insert id="batchInsert">&nbsp; &nbsp; insert into&nbsp; &nbsp; personal_tag(type, tag, create_time, open_account_id)&nbsp; &nbsp; values&nbsp; &nbsp; <foreach collection="list" item="item" index="index" separator=",">&nbsp; &nbsp; &nbsp; &nbsp; (#{item.type,jdbcType=INTEGER},&nbsp; &nbsp; &nbsp; &nbsp; #{item.tag,jdbcType=VARCHAR},&nbsp; &nbsp; &nbsp; &nbsp; #{item.createTme,jdbcType=TIMESTAMP},&nbsp; &nbsp; &nbsp; &nbsp; #{item.openAccountId,jdbcType=BIGINT})&nbsp; &nbsp; </foreach>ON DUPLICATE KEY UPDATE tag=tag

慕沐林林

ON DUPLICATE KEY UPDATE tag=tag把这一句改为ON DUPLICATE KEY UPDATE tag=#{item.tag}

蝴蝶刀刀

BindingException,那么一定是Mybatis遍历你Collections的时候,有个对象取不到item属性,也许你放了一个null对象,请检查你的List<Object>

翻翻过去那场雪

<insert id="batchInsert" parameterType="java.util.List">&nbsp; &nbsp; insert into&nbsp; &nbsp; personal_tag(type, tag, create_time, open_account_id)&nbsp; &nbsp; values&nbsp; &nbsp; <foreach collection="list" item="item" separator=",">&nbsp; &nbsp; &nbsp; &nbsp; (#{item.type,jdbcType=INTEGER},&nbsp; &nbsp; &nbsp; &nbsp; #{item.tag,jdbcType=VARCHAR},&nbsp; &nbsp; &nbsp; &nbsp; #{item.createTme,jdbcType=TIMESTAMP},&nbsp; &nbsp; &nbsp; &nbsp; #{item.openAccountId,jdbcType=BIGINT})&nbsp; &nbsp; </foreach>ON DUPLICATE KEY UPDATE tag=tag</insert>在insert标签加参数类型呢修改下参数别名呢,还有接口的参数名,下面是我们之前的项目里的void addRiskItemBatch(@Param("lstItem")List<FastRiskItemInfo> lstItem);<!-- 批量添加条款 --><insert id="addRiskItemBatch" useGeneratedKeys="true" parameterType="java.util.List">&nbsp;&nbsp;&nbsp; &nbsp; <selectKey resultType="long" keyProperty="id" order="AFTER">&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; SELECT&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; LAST_INSERT_ID()&nbsp;&nbsp;&nbsp; &nbsp; </selectKey>&nbsp; &nbsp; insert into fast_risk_item_info (query_id, risk_item_id,&nbsp;&nbsp; &nbsp; &nbsp; item_no, item_name, item_price,&nbsp;&nbsp; &nbsp; &nbsp; status, create_time)&nbsp; &nbsp; values&nbsp;&nbsp;&nbsp; &nbsp; <foreach collection="lstItem" item="item" index="index" separator="," >&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (#{item.queryId,jdbcType=INTEGER}, #{item.riskItemId,jdbcType=INTEGER},&nbsp;&nbsp; &nbsp; &nbsp; #{item.itemNo,jdbcType=VARCHAR}, #{item.itemName,jdbcType=VARCHAR}, #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{item.itemPrice,jdbcType=DECIMAL},&nbsp;&nbsp; &nbsp; &nbsp; #{item.status,jdbcType=INTEGER}, #{item.createTime,jdbcType=VARCHAR})&nbsp; &nbsp; </foreach>&nbsp;&nbsp;&nbsp; &nbsp;</insert>
随时随地看视频慕课网APP

相关分类

Java
我要回答