MyBatis批量新增和更新

之前有开发任务一个接口里面有大量的数据新增和更新操作,导致十分缓慢。使用了批量操作之后速度有明显提升,几乎百倍千倍的速度提升。

博主之前统计过,通过普通接口一次数据库插入大概需要200ms,对于大量新增或更新操作的情况,数据库批量操作是十分有必要的。废话不多说,直接上代码。

博主的resultMap如下:

<resultMap id="BaseResultMap" type="com.luo.domain.Words" >
    <id column="word_no" property="wordNo" jdbcType="BIGINT" />
    <result column="value" property="value" jdbcType="VARCHAR" />
    <result column="filed_class" property="filedClass" jdbcType="VARCHAR" />
    <result column="pinyin" property="pinyin" jdbcType="VARCHAR" />
    <result column="synonym" property="synonym" jdbcType="VARCHAR" />
    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
    <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
    <result column="operator_no" property="operatorNo" jdbcType="VARCHAR" />
    <result column="src_channel" property="srcChannel" jdbcType="VARCHAR" />
    <result column="latest_operation" property="latestOperation" jdbcType="VARCHAR" />
    <result column="versions" property="versions" jdbcType="BIGINT" />
    <result column="file_index" property="fileIndex" jdbcType="BIGINT" />
    <result column="charac_class" property="characClass" jdbcType="VARCHAR" />
    <result column="weight" property="weight" jdbcType="INTEGER" />
</resultMap>

批量新增

<insert id="addWordsByList" parameterType="java.util.List">
    insert into words (word_no, value, filed_class, 
      pinyin, synonym, create_date, 
      update_date, operator_no, src_channel, 
      latest_operation, versions, file_index, 
      charac_class, weight)
    values 
    <foreach collection="list" item="item" index="index" separator="," >
        (#{item.wordNo},#{item.value},#{item.filedClass},#{item.pinyin},
        #{item.synonym},#{item.createDate},#{item.updateDate},#{item.operatorNo},
        #{item.srcChannel},#{item.latestOperation},#{item.versions},#{item.fileIndex},
        #{item.characClass},#{item.weight})
    </foreach>
</insert>

接口:

public void addWordsByList(List<Words> wordsList);

批量更新

批量更新必须在添加如下数据库连接配置:&allowMultiQueries=true,否则会报SQL格式错误

比如MySQL:

jdbc:MySQL://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
<update id="updateWordsByList"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" separator=";">
       update words
       <set >
          <if test="item.value != null" >
            value = #{item.value,jdbcType=VARCHAR},
          </if>
          <if test="item.filedClass != null" >
            filed_class = #{item.filedClass,jdbcType=VARCHAR},
          </if>
          <if test="item.pinyin != null" >
            pinyin = #{item.pinyin,jdbcType=VARCHAR},
          </if>
          <if test="item.synonym != null" >
            synonym = #{item.synonym,jdbcType=VARCHAR},
          </if>
          <if test="item.createDate != null" >
            create_date = #{item.createDate,jdbcType=TIMESTAMP},
          </if>
          <if test="item.updateDate != null" >
            update_date = #{item.updateDate,jdbcType=TIMESTAMP},
          </if>
          <if test="item.operatorNo != null" >
            operator_no = #{item.operatorNo,jdbcType=VARCHAR},
          </if>
          <if test="item.srcChannel != null" >
            src_channel = #{item.srcChannel,jdbcType=VARCHAR},
          </if>
          <if test="item.latestOperation != null" >
            latest_operation = #{item.latestOperation,jdbcType=VARCHAR},
          </if>
          <if test="item.versions != null" >
            versions = #{item.versions,jdbcType=BIGINT},
          </if>
          <if test="item.fileIndex != null" >
            file_index = #{item.fileIndex,jdbcType=BIGINT},
          </if>
          <if test="item.characClass != null" >
            charac_class = #{item.characClass,jdbcType=VARCHAR},
          </if>
          <if test="item.weight != null" >
            weight = #{item.weight,jdbcType=INTEGER},
          </if>
        </set>
        where word_no = #{item.wordNo,jdbcType=BIGINT}
    </foreach>       
</update>

接口:

public void updateWordsByList(List<Words> wordsList);
  • 13
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值