zoukankan      html  css  js  c++  java
  • MyBatis批量插入数据

    一、批量插入

    1.1 一条语句

    1.1.1、说明和注意

    数据库支持单条 INSERT 语句有多组值。只需每组值用一对圆括号括起来,多组值之间用逗号分隔即可。

    说明和注意:

    1. DAO 层传入的是 List,List 里一个元素代表一条数据,同时 List 放入 Map 中;
    2. XML 中使用 foreach 遍历,每条数据中的元素全部用括号括起来,多条数据之间用逗号分割;
    3. 如果 List 为空,MyBatis 拼接的 SQL 会没有 value 部分,数据库会报错。故在批量插入之前做好数据判断。

    1.1.2、代码示例

    1.1.2.1 DAO 部分

    @Override
    public Integer batchInsert(List<VirtualOrderStatistics> virtualOrderStatisticsList) {
        Map<String, Object> queryMap = Maps.newHashMap();
        queryMap.put("virtualOrderStatisticsList", virtualOrderStatisticsList);
        return this.insert(this.getNameSpace("batchInsert"), queryMap);
    }
    

    1.1.2.2 SQL 部分

    <insert id="batchInsert" parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="id">
        insert into virtual_order_statistics
        (id, count_date, order_count, vendor_id, business_code, time_interval,
          order_success_count, order_fail_count, order_cancel_count,order_refund_count,time_out,
          recharge_time_out, recharge_success_time_out,
          recharge_fail_time_out, recharge_no_response_time_out,
          is_deleted, created, modified)
        values
        <foreach collection="virtualOrderStatisticsList" item="item" index="i" separator=",">
            (#{item.id,jdbcType=INTEGER}, #{item.countDate,jdbcType=TIMESTAMP}, #{item.orderCount,jdbcType=INTEGER}, #{item.vendorId,jdbcType=INTEGER}, #{item.businessCode,jdbcType=VARCHAR}, #{item.timeInterval,jdbcType=INTEGER},
            #{item.orderSuccessCount,jdbcType=INTEGER}, #{item.orderFailCount,jdbcType=INTEGER}, #{item.orderCancelCount,jdbcType=INTEGER}, #{item.orderRefundCount,jdbcType=INTEGER},#{item.timeOut,jdbcType=INTEGER},
            #{item.rechargeTimeOut,jdbcType=INTEGER}, #{item.rechargeSuccessTimeOut,jdbcType=INTEGER},
            #{item.rechargeFailTimeOut,jdbcType=INTEGER}, #{item.rechargeNoResponseTimeOut,jdbcType=INTEGER},
            0, now(), now())
        </foreach>
    </insert>
    

    1.1.3、参考:

    1. MyBatis 批量插入数据 (MySql) - Admol - 博客园

    1.2 多条语句

    方法同上,区别在于,上面的SQL部分只是使用 foreach拼接数据部分,实际我们可以使用foreach拼接多条SQL语句,每条语句之间使用;分割即可。

    <insert id="batchInsert" parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="id">
        <foreach collection="virtualOrderStatisticsList" item="item" index="i" separator=";">
    		insert into virtual_order_statistics
    		(id, count_date, order_count, vendor_id, business_code, time_interval,
    		  order_success_count, order_fail_count, order_cancel_count,order_refund_count,time_out,
    		  recharge_time_out, recharge_success_time_out,
    		  recharge_fail_time_out, recharge_no_response_time_out,
    		  is_deleted, created, modified)
    		values
    		(#{item.id,jdbcType=INTEGER}, #{item.countDate,jdbcType=TIMESTAMP}, #{item.orderCount,jdbcType=INTEGER}, #{item.vendorId,jdbcType=INTEGER}, #{item.businessCode,jdbcType=VARCHAR}, #{item.timeInterval,jdbcType=INTEGER},
            #{item.orderSuccessCount,jdbcType=INTEGER}, #{item.orderFailCount,jdbcType=INTEGER}, #{item.orderCancelCount,jdbcType=INTEGER}, #{item.orderRefundCount,jdbcType=INTEGER},#{item.timeOut,jdbcType=INTEGER},
            #{item.rechargeTimeOut,jdbcType=INTEGER}, #{item.rechargeSuccessTimeOut,jdbcType=INTEGER},
            #{item.rechargeFailTimeOut,jdbcType=INTEGER}, #{item.rechargeNoResponseTimeOut,jdbcType=INTEGER},
            0, now(), now())
        </foreach>
    </insert>
    

    二、批量更新

    思路同上述【1.2 多条语句】插入情况,具体自行结合需要拼接。

  • 相关阅读:
    Database mirroring connection error 4 'An error occurred while receiving data: '10054(An existing connection was forcibly closed by the remote host.)
    [转载]——Automatic Tuning of Undo_retention Causes Space Problems (文档 ID 420525.1)
    [转载]——Full UNDO Tablespace In 10gR2 and above (文档 ID 413732.1)
    SQL Server Agent作业执行CmdExec(bat)命令报权限问题
    SQL Server有意思的数据类型隐式转换问题
    pymssql的Connection相关特性浅析
    pymssql默认关闭自动模式开启事务行为浅析
    Azure上MySQL的离线备份:将备份拷贝到Azure Blob上
    crontab中部署Python脚本注意事项
    TypeError: expected string or bytes-like object
  • 原文地址:https://www.cnblogs.com/buwuliao/p/11346549.html
Copyright © 2011-2022 走看看