mybatis批量插入与更新

由于系统中一次性需要导入1000条数据,为防止超时以及长时间占用数据库连接,采取了批量更新插入。

1.后台代码每50条,与数据库进行一次交互

            //批量插入/更新
            int dataLimitNum = 50;// 限制分批条数
            int i = 0;
            while (i < list.size()) {
                int from = i;
                int to = i + dataLimitNum;
                if (to >= list.size()) {
                    to = list.size();
                }

                List<User> partList = list.subList(from, to);
                if (partList.isEmpty()) {
                    break;
                }
                i = to;
                userDao.insertSelectiveBatch(partList);
            }
  2.批量插入

<insert id="insertSelectiveBatch" parameterType="net.su.User">
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
      insert into User
      <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="item.id != null">
          ID,
        </if>
        <if test="item.userName!= null">
          USER_NAME,
        </if>
        <if test="item.age!= null">
          AGE,
        </if>
        <if test="item.mobile!= null">
          Mobile,
        </if>
        <if test="item.created != null">
          CREATED,
        </if>
        <if test="item.createBy != null">
          CREATE_BY,
        </if>
        <if test="item.updated != null">
          UPDATED,
        </if>
        <if test="item.updateBy != null">
          UPDATE_BY,
        </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="item.id != null">
          #{item.id,jdbcType=BIGINT},
        </if>
        <if test="item.userName!= null">
          #{item.userName,jdbcType=VARCHAR},
        </if>
        <if test="item.age!= null">
          #{item.age,jdbcType=BIGINT},
        </if>
        <if test="item.mobile!= null">
          #{item.mobile,jdbcType=VARCHAR},
        </if>
        <if test="item.created != null">
          #{item.created,jdbcType=TIMESTAMP},
        </if>
        <if test="item.createBy != null">
          #{item.createBy,jdbcType=VARCHAR},
        </if>
        <if test="item.updated != null">
          #{item.updated,jdbcType=TIMESTAMP},
        </if>
        <if test="item.updateBy != null">
          #{item.updateBy,jdbcType=VARCHAR},
        </if>
      </trim>
    </foreach>
  </insert>

3.批量更新

<update id="updateByPrimaryKeyBatch" parameterType="net.su.User">
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
      update USER
      <set>
        <if test="item.id!= null">
          ID= #{item.id,jdbcType=BIGINT},
        </if>
        <if test="item.userName!= null">
          UserName= #{item.userName,jdbcType=VARCHAR},
        </if>
        <if test="item.age!= null">
          AGE= #{item.age,jdbcType=BIGINT},
        </if>
        <if test="item.mobile!= null">
          Mobile= #{item.mobile,jdbcType=VARCHAR},
        </if>
         <if test="item.created != null">
          CREATED = #{item.created,jdbcType=TIMESTAMP},
        </if>
        <if test="item.createBy != null">
          CREATE_BY = #{item.createBy,jdbcType=VARCHAR},
        </if>
        <if test="item.updated != null">
          UPDATED = #{item.updated,jdbcType=TIMESTAMP},
        </if>
        <if test="item.updatedBy != null">
          UPDATED_BY = #{item.updatedBy,jdbcType=VARCHAR},
        </if>
        </set>
      where ID = #{item.id,jdbcType=BIGINT} 
    </foreach>
  </update>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值