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

博主之前统计过,通过普通接口一次数据库插入大概需要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批量新增和更新的更多相关文章

  1. mybatis批量新增或更新

    mysql中在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE时,向数据库中插入一条记录: 若插入数据的主键值/ UNIQUE KEY 已经在表中存在,则执行更新操作(UPDA ...

  2. mysql批量新增或者更新

    1.批量更新或者新增 1.单个新增或者更新 keyProperty新增完之后返回Id值

  3. mybatis 批量新增-批量修改-批量删除操作

    mapper.xml <!-- 批量新增 --> <insert id="saveBatch" parameterType="java.util.Lis ...

  4. Mybatis 批量插入和更新小例

    SpringBoot配置Mybatis前文有博文,数据库mysql: package com.example.demo.biz.dto; public class User { private int ...

  5. Oracle+Mybatis批量插入,更新和删除

    1.插入 (1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过) <insert id="inse ...

  6. mybatis批量插入和更新

    批量插入 <insert id="add" parameterType="java.util.List"> insert all <forea ...

  7. Mybatis批量添加、更新小结

    虽然是很基础的东西,不过难免会忘记,所以写个笔记巩固一下,顺便分享. 实体类: @Data public class EventOrder { ​ private Long id; ​ private ...

  8. [置顶] mybatis批量新增系列之有主键的表的批量新增

    前面介绍了无主键的表的批量插入,文章地址:http://blog.csdn.net/zhouxiaoyun0228/article/details/9980181 但是在开发中往往许多的表是需要主键的 ...

  9. mybatis批量新增报错 BadSqlGrammarException

    org.springframework.jdbc.BadSqlGrammarException: ### Error updating database. Cause: com.mysql.jdbc. ...

随机推荐

  1. create-keyframe-animation-js实现css3动画

    使用JavaScript在浏览器中动态生成CSS关键帧动画. enter(el, done) { // 设置动画帧数 let animation = { 0: { transform: `transl ...

  2. JavaScript的屏幕对象

    screen 屏幕对象 反映了当前用户的屏幕设置. width 返回屏幕的宽度(像素数). height 返回屏幕的高度. availWidth 返回屏幕的可用宽度(除去了一些不自动隐藏的类似任务栏的 ...

  3. MyBatis(2)——MyBatis 深入学习

    编写日志输出环境配置文件 在开发过程中,最重要的就是在控制台查看程序输出的日志信息,在这里我们选择使用 log4j 工具来输出: 准备工作: 将[MyBatis]文件夹下[lib]中的 log4j 开 ...

  4. Thread源码剖析

    前言 昨天已经写了: 多线程三分钟就可以入个门了! 如果没看的同学建议先去阅读一遍哦~ 在写文章之前通读了一遍<Java 核心技术 卷一>的并发章节和<Java并发编程实战>前 ...

  5. [JSOI2007]祖码Zuma

    题目描述 这是一个流行在Jsoi的游戏,名称为祖玛. 精致细腻的背景,外加神秘的印加音乐衬托,彷佛置身在古老的国度里面,进行一个神秘的游戏——这就是著名的祖玛游戏.祖玛游戏的主角是一只石青蛙,石青蛙会 ...

  6. weak_ptr解决shared_ptr环状引用所引起的内存泄漏[转]

    转载:http://blog.csdn.net/liuzhi1218/article/details/6993135 循环引用: 引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理 ...

  7. POJ1222熄灯问题

    千年老题,以前用枚举做,现在用高斯消元做 自由元直接做成0即可 #include<cstdio> #include<cstdlib> #include<algorithm ...

  8. hdu 5573Binary Tree

    Binary Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  9. [bzoj4763]雪辉&[bzoj4812][Ynoi2017]由乃打扑克

    来自FallDream的博客,未经允许,请勿转载,谢谢. cut掉部分题面. 给一个n个点的树,点有点权,有m次询问,每次询问多条链的并有多少种不同的点权以及它的mex mex就是一个集合中最小的没有 ...

  10. 如何在Google上下载高清原图

    在我们学习和生活中常常一些高清图片作为相关的素材,比如制作PPT.写博文.制作视频都需要大量图片.我们常常会在百度上下载一些图片,但是百度上提供的图片存在很多问题:存在水印.清晰度不够等.而Googl ...