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);















相关文章
|
8天前
|
Apache
修改mybatis-plus更新策略
修改mybatis-plus更新策略
12 0
|
10天前
|
Java 关系型数据库 MySQL
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
|
3月前
|
测试技术 数据库
深入解析MyBatis-Plus中的逻辑删除功能及实例
深入解析MyBatis-Plus中的逻辑删除功能及实例
177 0
|
3月前
|
SQL 存储 Java
Mybatis练习(增加,删除,修改)
Mybatis练习(增加,删除,修改)
42 0
|
3月前
|
SQL Java 关系型数据库
Mybatis 实现动态批量修改
Mybatis 实现动态批量修改
43 0
|
5月前
mybatis-plus默认查询方式
mybatis-plus默认查询方式
59 0
|
5月前
MyBatis-Plus更新部分字段
MyBatis-Plus更新部分字段
202 0
|
6月前
|
SQL Java 数据库连接
Mybatis实战练习五【修改&删除一行数据】
Mybatis实战练习五【修改&删除一行数据】
|
9月前
|
SQL Java 关系型数据库
MyBatis 如何执行批量添加数据?
MyBatis 如何执行批量添加数据?
70 0
|
SQL Java 数据库连接
MyBatis-Plus批量新增方法
MyBatis-Plus 是基于 MyBatis 进行封装的一套优秀的持久层框架,它提供了丰富的便捷操作方法和强大的代码生成器,大大简化了 MyBatis 的使用。在 MyBatis-Plus 中,我们可以使用 insertBatchSomeColumn 方法来实现批量新增指定字段的操作。
1365 0