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

博主之前统计过,通过普通接口一次数据库插入大概需要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. 深入探究Lua的GC算法(下)-《Lua设计与实现》

    紧接着上一篇文章zblade:深入探究Lua的GC算法(上)-<Lua设计与实现> 这篇文章让我们收尾GC的具体后续操作.转载请标明出处:http://www.cnblogs.com/zb ...

  2. winform中的数据绑定

    1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[& ...

  3. 开发一款APP所需要的时间

    "要多少钱""要多少时间"这应该是一个企业在打算开发一款APP时问到最多的问题了.的确,现在的人不管做什么事情都讲究计划,更何况在这个时间就是金钱的时代,企业如 ...

  4. Java 异常基础详解

    目录 1. Java 中的异常 1.1 什么是异常? 1.2 什么是异常处理? 1.2.1 异常处理的优势 1.3 Java 异常类的层次结构 1.4 异常类型 1.5 检查和未检查异常之间的区别 1 ...

  5. Angular1.x使用小结

    之前工作以Angular1.x为主,主要做业务系统,以后工作中技术栈可能以vue为主,在此对Angular1.x的使用做一个简单总结,这里使用1.5+版本. 基本概念 1.依赖注入 依赖注入,在ang ...

  6. [HNOI 2016]大数

    Description 题库链接 给你一个长度为 \(n\) ,可含前导零的大数,以及一个质数 \(p\) . \(m\) 次询问,每次询问你一个大数的子区间 \([l,r]\) ,求出子区间中有多少 ...

  7. [HNOI2014]江南乐

    Description 小A是一个名副其实的狂热的回合制游戏玩家.在获得了许多回合制游戏的世界级奖项之后,小A有一天突然想起了他小时候在江南玩过的一个回合制游戏.    游戏的规则是这样的,首先给定一 ...

  8. [HNOI2015]接水果

    题目描述 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, 她觉得这个游戏太简单了,于是发明了一个更加难的版本. 首先有 ...

  9. bzoj 5248: [2018多省省队联测]一双木棋

    Description 菲菲和牛牛在一块n行m列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手.棋局开始时,棋盘上没有任何棋子, 两人轮流在格子上落子,直到填满棋盘时结束.落子的规则是:一个格子可以落子 ...

  10. 2015 多校联赛 ——HDU5386(暴力)

    Sample Input 1 3 5 2 2 1 2 3 3 2 1 3 3 3 3 3 3 3 3 3 3 H 2 3 L 2 2 H 3 3 H 1 3 L 2 3   Sample Output ...