MyBatis批量新增和更新

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);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2269.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 向量范数和矩阵范数[通俗易懂]

    向量范数和矩阵范数[通俗易懂]本文分别介绍了向量范数和矩阵范数的定义,以及几种常见的向量范数和矩阵范数

    2022年9月19日
    2
  • docker搭建LDAP统一用户认证

    docker搭建LDAP统一用户认证1 安装 LDAPdockerru dit p389 389 v data ldap ldap var lib ldap v data ldap slapd d etc ldap slapd d nameldap envLDA

    2025年7月4日
    2
  • 数据结构学习笔记(二)–ElemType是什么?

    数据结构学习笔记(二)–ElemType是什么?ElemType(也有的书上称之为elemtp)是数据结构的书上为了说明问题而用的一个词。它是elementtype(“元素的类型”)的简化体。 因为数据结构是讨论抽象的数据存储和算法的,一种结构中元素的类型不一定是整型、字符型、浮点型或者用户自定义类型,为了不重复说明,使用过程中用“elemtype”代表所有可能的数据类型,简单明了的概括了整体。在算法中,除特别说明外,规定ElemType的默

    2022年5月12日
    76
  • allure 报告[通俗易懂]

    allure 报告[通俗易懂]一、简介二、下载安装三、报告生成四、环境配置五、Python使用allure方法一、简介官方文档:https://docs.qameta.io/allure/二、下载安装1、linux下载安装先检查是否安装npm:whichnpm 未安装npm的话:curl–silent–locationhttps://rpm.nodesource.com/setup_10.x|bash- 安装:yuminstall-ynodejs …

    2022年7月26日
    21
  • 关联数据及其应用

    关联数据及其应用转载自:http://blog.sciencenet.cn/blog-357889-578799.html关联数据(LinkedData)是万维网的发明人——蒂姆•伯纳斯-李(TimBerners-Lee)——提出的一种万维网上发布数据的方式,可以看成语义Web的一种实现方式。它一般要求采用RDF数据模型,利用URI(统一资源标识符)命名数据实体,发布和部署实例数据和类数据

    2022年7月17日
    22
  • ZIlliqa团队关于分片、可扩展性和安全的智能合约的采访

    ZIlliqa团队关于分片、可扩展性和安全的智能合约的采访

    2021年7月1日
    65

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号