- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Mybatis中使用updateBatch进行批量更新由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
背景描述:通常如果需要一次更新多条数据有两个方式,(1)在业务代码中循环遍历逐条更新。(2)一次性更新所有数据(更准确的说是一条sql语句来更新所有数据,逐条更新的操作放到数据库端,在业务代码端展现的就是一次性更新所有数据)。两种方式各有利弊,下面将会对两种方式的利弊做简要分析,主要介绍第二种方式在mybatis中的实现.
逐条更新 。
这种方式显然是最简单,也最不容易出错的,即便出错也只是影响到当条出错的数据,而且可以对每条数据都比较可控,更新失败或成功,从什么内容更新到什么内容,都可以在逻辑代码中获取。代码可能像下面这个样子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
updatebatch(list<mydata> datas){
for
(mydata data : datas){
try
{
mydatadao.update(data);
//更新一条数据,mybatis中如下面的xml文件的update
}
catch
(exception e){
...
//如果更新失败可以做一些其他的操作,比如说打印出错日志等
}
}
}
//mybatis中update操作的实现
<update>
update mydata
set ...
where ...
</update>
|
这种方式最大的问题就是效率问题,逐条更新,每次都会连接数据库,然后更新,再释放连接资源(虽然通过连接池可以将频繁连接数据的效率大大提高,抗不住数据量大),这中损耗在数据量较大的时候便会体现出效率问题。这也是在满足业务需求的时候,通常会使用上述提到的第二种批量更新的实现(当然这种方式也有数据规模的限制,后面会提到).
sql批量更新 。
一条sql语句来批量更新所有数据,下面直接看一下在mybatis中通常是怎么写的(去掉mybatis语法就是原生的sql语句了,所有就没单独说sql是怎么写的).
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<update id=
"updatebatch"
parametertype=
"java.util.list"
>
update mydata_table
set status=
<foreach collection=
"list"
item=
"item"
index=
"index"
separator=
" "
open=
"case id"
close=
"end"
>
when #{item.id} then #{item.status}
</foreach>
where id in
<foreach collection=
"list"
index=
"index"
item=
"item"
separator=
","
open=
"("
close=
")"
>
#{item.id,jdbctype=bigint}
</foreach>
</update>
|
其中when...then...是sql中的"switch" 语法。这里借助mybatis的<foreach>语法来拼凑成了批量更新的sql,上面的意思就是批量更新id在updatebatch参数所传递list中的数据的status字段。还可以使用<trim>实现同样的功能,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<update id=
"updatebatch"
parametertype=
"java.util.list"
>
update mydata_table
<trim prefix=
"set"
suffixoverrides=
","
>
<trim prefix=
"status =case"
suffix=
"end,"
>
<foreach collection=
"list"
item=
"item"
index=
"index"
>
when id=#{item.id} then #{item.status}
</foreach>
</trim>
</trim>
where id in
<foreach collection=
"list"
index=
"index"
item=
"item"
separator=
","
open=
"("
close=
")"
>
#{item.id,jdbctype=bigint}
</foreach>
</update>
|
<trim>属性说明 。
1.prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容 。
2.如果同时有prefixoverrides,suffixoverrides 表示会用prefix,suffix覆盖overrides中的内容.
3.如果只有prefixoverrides,suffixoverrides 表示删除开头的或结尾的xxxoverides指定的内容.
上述代码转化成sql如下
1
2
3
4
5
6
7
|
update mydata_table
set status =
case
when id = #{item.id} then #{item.status}
//此处应该是<foreach>展开值
...
end
where id in (...);
|
当然这是最简单的批量更新实现,有时候可能需要更新多个字段,那就需要将 。
1
2
3
4
5
|
<trim prefix=
"status =case"
suffix=
"end,"
>
<foreach collection=
"list"
item=
"item"
index=
"index"
>
when id=#{item.id} then #{item.status}
</foreach>
</trim>
|
复制拷贝多次,更改prefix和when...then...的内容即可.而如果当需要为某个字段设置默认值的时候可以使用else 。
1
2
3
4
5
6
|
<trim prefix=
"status =case"
suffix=
"end,"
>
<foreach collection=
"list"
item=
"item"
index=
"index"
>
when id=#{item.id} then #{item.status}
</foreach>
else
default_value
</trim>
|
。
还有更常见的情况就是需要对要更新的数据进行判断,只有符合条件的数据才能进行更新,这种情况可以这么做
1
2
3
4
5
6
7
|
<trim prefix=
"status =case"
suffix=
"end,"
>
<foreach collection=
"list"
item=
"item"
index=
"index"
>
<
if
test=
"item.status !=null and item.status != -1"
>
when id=#{item.id} then #{item.status}
</
if
>
</foreach>
</trim>
|
这样的话只有要更新的list中status != null && status != -1的数据才能进行status更新.其他的将使用默认值更新,而不会保持原数据不变.如果要保持原数据不变呢?即满足条件的更新,不满足条件的保持原数据不变,简单的来做就是再加一个<if>,因为mybatis中没有if...else...语法,但可以通过多个<if>实现同样的效果,如下
1
2
3
4
5
6
7
8
9
10
|
<trim prefix=
"status =case"
suffix=
"end,"
>
<foreach collection=
"list"
item=
"item"
index=
"index"
>
<
if
test=
"item.status !=null and item.status != -1"
>
when id=#{item.id} then #{item.status}
</
if
>
<
if
test=
"item.status == null or item.status == -1"
>
when id=#{item.id} then mydata_table.status
//这里就是原数据
</
if
>
</foreach>
</trim>
|
整体批量更新的写法如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<update id=
"updatebatch"
parametertype=
"java.util.list"
>
update mydata_table
<trim prefix=
"set"
suffixoverrides=
","
>
<trim prefix=
"status =case"
suffix=
"end,"
>
<foreach collection=
"list"
item=
"item"
index=
"index"
>
<
if
test=
"item.status !=null and item.status != -1"
>
when id=#{item.id} then #{item.status}
</
if
>
<
if
test=
"item.status == null or item.status == -1"
>
when id=#{item.id} then mydata_table.status
//原数据
</
if
>
</foreach>
</trim>
</trim>
where id in
<foreach collection=
"list"
index=
"index"
item=
"item"
separator=
","
open=
"("
close=
")"
>
#{item.id,jdbctype=bigint}
</foreach>
</update>
|
这种批量跟心数据库的方式可以在一次数据库连接中更新所有数据,避免了频繁数据库建立和断开连接的开销,可以很大程度的提高数据更新效率。但是这样的问题是如果这个过程中更新出错,将很难知道具体是哪个数据出错,如果使用数据自身的事务保证,那么一旦出错,所有的更新将自动回滚。而且通常这种方式也更容易出错。因此通常的使用的方案是进行折中,也就是一次批量更新一部分(分页进行更新,比如说一共有1000条数据,一次更新100条)。这样可以分担出错的概率,也更容易定位到出错的位置.
当然如果数据量确实很大的时候,这种批量更新也一样会导致更新效率低下(比如说一次更新100条,那如果10亿条数据呢,一样要批量更新1000万次,建立和断开1000万次数据库,这个效率是无法承受的)。这时候也许只能考虑其他方案了,比如引入缓存机制等.
总结 。
以上所述是小编给大家介绍的mybatis中使用updatebatch进行批量更新,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:https://blog.csdn.net/xyjawq1/article/details/74129316 。
最后此篇关于Mybatis中使用updateBatch进行批量更新的文章就讲到这里了,如果你想了解更多关于Mybatis中使用updateBatch进行批量更新的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!