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. ...
随机推荐
- IndentationError : expected an indented block
IndentationError:在python的条件语句出现 expected an indented block问题 是指缩进问题,比如for循环里面的print前面需要四个空格. Python语 ...
- Javascript闭包(Closure)
1.Javascript特殊的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量. function f1() { var ...
- jqurey datatables属性
$('selector').dataTable( { /* * 默认为true * 是否自动计算列宽,计算列宽会花费一些时间,如果列宽通过aoColumns传递,可以关闭该属性作为优化 */ &quo ...
- CentOS下实用的网络管理工具
昨天在家把在家待业的笔记本装上了CentOS 7最小版本,今天拿到公司发现没法改Wifi链接,在解决的过程中发现了一个TUI工具非常好用,在此分享给大家. 1. 安装 sudo yum install ...
- WebGL文字渲染的那些问题
THREE.js开发的应用运行在iphone5下发现有些时候会崩溃,跟了几天发现是因为Sprite太多频繁更新纹理占用显存导致的.通常解决纹理频繁更新问题就要用到one draw all方法,放到纹理 ...
- crypto必知必会
crypto必知必会 最近参加了个ctf比赛,在i春秋,南邮方面刷了一些crypto密码学题目,从中也增长了不少知识,在此关于常见的密码学知识做个小总结! Base编码 Base编码中用的比较多的是b ...
- Java基础详解
从写Java系列的第一篇到现在已经三个月了,因为在网络上或书籍中没有见到一些很适合初学者的学习流程,所以下决心自己写一写,也当作回顾一下Java的知识.网上有许多Java教程之类的内容,都是从概念起步 ...
- ionic轮播图
根据自带的滑动图,只需要添加加红字体 <ion-view view-title="活动详情" class="align-title-center"> ...
- 二 Djano模型层之模型字段选项
字段选项 以下参数是全部字段类型都可用的,而且是可选的 null 如果为True,Django将在数据库中将空值存储为NULL.默认值为False 对于字符串字段,如果设置了null=True意味着& ...
- JavaScript的作用;JS常见的三种对话框;==和===的区别;函数内部参数数组arguments在函数内部打印实参;JS的误区:没有块级作用域
JS:客户端(浏览器)脚本语言 弱类型 基于原型 事件驱动 不需要编译(直接运行) JS的作用:表单验证,减轻服务端的压力 添加页面动画效果 动态更改页面内容 Ajax网络请求 (一)常见的对 ...