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年10月9日
    2
  • 对CAB文件进行数字签名

    对CAB文件进行数字签名

    2021年11月16日
    41
  • XML格式化工具_u盘格式化恢复工具

    XML格式化工具_u盘格式化恢复工具做接口开发的时候,往往接受参数或返回值是一个XML的字符串。如下图,不方便辨识两种方法,1.将它保存为xxx.xml,然后用浏览器打开。这种方法稍微有些麻烦。2.使用UltraEdit工具

    2022年8月4日
    7
  • MongoDB配置文件mongod.conf

    MongoDB配置文件mongod.conf

    2021年11月22日
    44
  • PyTorch实现MLP的两种方法,以及nn.Conv1d, kernel_size=1和nn.Linear的区别

    PyTorch实现MLP的两种方法,以及nn.Conv1d,kernel_size=1和nn.Linear的区别MLP(Multi-layerperceptron)实现MLP结构方法1:nn.Linear方法2:nn.Conv1d&kernel_size=1nn.Conv1d,kernel_size=1与nn.Linear不同MLP(Multi-layerperceptron)实现最近在看PointNet论文,其主要思想为利用MLP结构学习点云特征,并进行全局池化(构造一个对称函数,

    2022年4月6日
    177
  • 公网ntp服务器地址(网站服务器)

    阿里云NTP服务器ntp1.aliyun.comntp2.aliyun.comntp3.aliyun.comntp4.aliyun.comntp5.aliyun.comntp6.aliyun.comntp7.aliyun.com腾讯云NTP服务器time1.cloud.tencent.comtime2.cloud.tencent.comtime3.cloud.ten…

    2022年4月11日
    62

发表回复

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

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