mybatis批量更新的两种实现方式

mapper.xml文件,后台传入一个对象集合,另外如果是mysql数据库,一点在配置文件上加上&allowMultiQueries=true,这样才可以执行多条sql,不过MySQL有缓存最大限制是1000条,在Java中要控制循环次数,以下为mysql:

<update id="batchUpdate" parameterType="java.util.List"> 
      <foreach separator=";" index="index" item="item" collection="list" close="" open=""> 
      update sys_group set level = #{item.level,jdbcType=INTEGER}
       where group_id = #{item.groupId,jdbcType=INTEGER}
      </foreach> 
  </update>

 带if判断的写法:

<update id="updateBatch" parameterType="java.util.List" >
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update standard_relation
            <set >
                <if test="item.standardFromUuid != null" >
                    standard_from_uuid = #{item.standardFromUuid,jdbcType=VARCHAR},
                </if>
                <if test="item.standardToUuid != null" >
                    standard_to_uuid = #{item.standardToUuid,jdbcType=VARCHAR},
                </if>
                <if test="item.gmtModified != null" >
                    gmt_modified = #{item.gmtModified,jdbcType=TIMESTAMP},
                </if>
            </set>
            where id = #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>

 

如果是oracle数据库则写法有不同:

<update id="batchUpdateRepayPlan" parameterType="java.util.List">
     begin   
      <foreach separator=";" index="index" item="item" collection="list" close="" open="">   
      update t_ba_repay_plan 
      <set>
       <if test="item.interest !=null">
           REPAY_INTEREST = #{item.interest,jdbcType=VARCHAR},
       </if>
       <if test="item.nimm !=null">
           REPAY_NIMM = #{item.nimm,jdbcType=CHAR}
       </if>
      </set>  
       where IOU_CODE = #{item.iouCode}  
      </foreach> 
      ;end; 
</update>

 本文转自:https://blog.csdn.net/pangliang_csdn/article/details/68945750

posted @ 2018-08-30 11:27  ppjj  阅读(652)  评论(0编辑  收藏  举报