mysql - JAVA MyBatis批量操作(插入)时异常
PHP中文网
PHP中文网 2017-04-18 10:06:33
[Java讨论组]
PHP中文网
PHP中文网

认证0级讲师

全部回复(6)
迷茫

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

天蓬老师

在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。

<insert id="batchInsert">
    insert into
    personal_tag(type, tag, create_time, open_account_id)
    values
    <foreach collection="list" item="item" index="index" separator=",">
        (#{item.type,jdbcType=INTEGER},
        #{item.tag,jdbcType=VARCHAR},
        #{item.createTme,jdbcType=TIMESTAMP},
        #{item.openAccountId,jdbcType=BIGINT})
    </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">
    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>

在insert标签加参数类型呢


修改下参数别名呢,还有接口的参数名,下面是我们之前的项目里的
void addRiskItemBatch(@Param("lstItem")List<FastRiskItemInfo> lstItem);

<!-- 批量添加条款 -->
<insert id="addRiskItemBatch" useGeneratedKeys="true" parameterType="java.util.List">  
    <selectKey resultType="long" keyProperty="id" order="AFTER">  
        SELECT  
        LAST_INSERT_ID()  
    </selectKey>
    insert into fast_risk_item_info (query_id, risk_item_id, 
      item_no, item_name, item_price, 
      status, create_time)
    values  
    <foreach collection="lstItem" item="item" index="index" separator="," >  
        (#{item.queryId,jdbcType=INTEGER}, #{item.riskItemId,jdbcType=INTEGER}, 
      #{item.itemNo,jdbcType=VARCHAR}, #{item.itemName,jdbcType=VARCHAR}, #                       {item.itemPrice,jdbcType=DECIMAL}, 
      #{item.status,jdbcType=INTEGER}, #{item.createTime,jdbcType=VARCHAR})
    </foreach>  
   </insert>
迷茫

你 好 ,解决 没有 ,我的也遇到了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号