前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MyBatis批量新增和更新

MyBatis批量新增和更新

作者头像
全栈程序员站长
发布2021-04-07 11:22:56
1.3K0
发布2021-04-07 11:22:56
举报
文章被收录于专栏:全栈程序员必看

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

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

博主的resultMap如下:

代码语言:javascript
复制
<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>

批量新增

代码语言:javascript
复制
<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>

接口:

代码语言:javascript
复制
public void addWordsByList(List<Words> wordsList);

批量更新

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

比如MySQL:

代码语言:javascript
复制
jdbc:MySQL://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
代码语言:javascript
复制
<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>

接口:

代码语言:javascript
复制
public void updateWordsByList(List<Words> wordsList);

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020年11月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 批量新增
  • 批量更新
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档