MyBatis批量新增和更新

Posted 小宝鸽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

以上是关于MyBatis批量新增和更新的主要内容,如果未能解决你的问题,请参考以下文章

on duplicate key update 的用法说明(解决批量操作数据,有就更新,没有就新增)mybatis批量操作数据更新和添加

mybatis学习之路----mysql批量新增数据

mybatis 怎么批量更新操作

mybatis新增和批量新增

mybatis 批量操作数据

mybatis批量新增