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);
MyBatis批量新增和更新的更多相关文章
- mybatis批量新增或更新
mysql中在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE时,向数据库中插入一条记录: 若插入数据的主键值/ UNIQUE KEY 已经在表中存在,则执行更新操作(UPDA ...
- mysql批量新增或者更新
1.批量更新或者新增 1.单个新增或者更新 keyProperty新增完之后返回Id值
- mybatis 批量新增-批量修改-批量删除操作
mapper.xml <!-- 批量新增 --> <insert id="saveBatch" parameterType="java.util.Lis ...
- Mybatis 批量插入和更新小例
SpringBoot配置Mybatis前文有博文,数据库mysql: package com.example.demo.biz.dto; public class User { private int ...
- Oracle+Mybatis批量插入,更新和删除
1.插入 (1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过) <insert id="inse ...
- mybatis批量插入和更新
批量插入 <insert id="add" parameterType="java.util.List"> insert all <forea ...
- Mybatis批量添加、更新小结
虽然是很基础的东西,不过难免会忘记,所以写个笔记巩固一下,顺便分享. 实体类: @Data public class EventOrder { private Long id; private ...
- [置顶] mybatis批量新增系列之有主键的表的批量新增
前面介绍了无主键的表的批量插入,文章地址:http://blog.csdn.net/zhouxiaoyun0228/article/details/9980181 但是在开发中往往许多的表是需要主键的 ...
- mybatis批量新增报错 BadSqlGrammarException
org.springframework.jdbc.BadSqlGrammarException: ### Error updating database. Cause: com.mysql.jdbc. ...
随机推荐
- 使用vue-cli快速搭建大型单页面应用开发环境
工作环境:terminal,已经全局安装了vue(可使用npm install -g vue) 全局安装vue-cli npm install -g vue-cli 创建一个基于webpack模板的项 ...
- 扩展第二屏幕发生Out Of Range及扩展后耳机没声音解决方案
新年好\(^o^)/~ 拓展屏幕这种事情.其实很简单的.无非就分辨率跟刷新频率.有时候一接好就可以了.有时候怎么也弄不出来.我也是搞了蛮久.昨天突然弄通了.小记一下说不定能帮到同需求人. [设备] 主 ...
- countUp.js-让数字动起来
先上一段示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- pandas用法大全
pandas用法大全 一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as np import pandas as pd12 2. ...
- [SDOI 2008]沙拉公主的困惑
Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...
- [HNOI 2015]落忆枫音
Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出 这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂……我们 ...
- [ZJOI2017]树状数组
Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道 基础的树状数组题.给出一个长度为 n 的数组 A,初始值都为 0,接下来 ...
- HDU3311Dig The Wells
给定N个寺庙,和M个另外的地方. 然后给定点权,表示在这个点挖水井需要的代价. 再给定边权,为建造无向边i,j的代价. 然后求怎样弄最小的代价使得前N个点,就是寺庙都能从挖的井里得到水. 输入输出格式 ...
- ●BZOJ 1096 [ZJOI2007]仓库建设
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1096 题解: 斜率优化DP $(d_i:i 位置到1位置的距离,p_i:i位置的成品数量,c ...
- ●BZOJ 2005 NOI 2010 能量采集
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2005 题解: 一个带有容斥思想的递推.%%% 首先,对于一个点 (x,y) 在路径 (0,0 ...