当前位置:网站首页>MySQL 巨坑:update 更新慎用影响行数做判断!!!
MySQL 巨坑:update 更新慎用影响行数做判断!!!
2022-07-05 12:39:00 【Java技术栈】
来源:cnblogs.com/yjmyzz/p/13562182.html
业务系统中,使用update语句更新数据是再正常不过的场景,我们也经常通过update更新的行数,来做一些业务判断,类似下面的伪代码:(mybatis + mysql 场景)
if (xxxMapper.updateByPrimaryKeySelective(entity)>0){
//更新成功,做其它业务处理
}但是这里有一个坑,mysql中update影响行数>0是有条件的,假如有一张表:

里面只有一条记录(ID=1),我们用update更新一把ID=1的这条记录

这一行成功更新,影响行数为1,理所当然,然后把这条update语句再执行一次:

这时候,返回的影响行数为0,也就是说,当待更新的记录与原始记录旧值相同时,mysql其实并不会做任何更新。。
换言之,如果上游传过来的数据,与数据库本身的旧值相等,没有变化时,update语句影响行数为0。这与另一种场景:"更新一条并不存在的记录,影响行数返回0" 无法区分。

结论:不要使用update语句的影响行数做重要的业务判断!
近期热文推荐:
1.1,000+ 道 Java面试题及答案整理(2022最新版)
4.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
- SAP UI5 DynamicPage 控件介绍
- Redis master-slave configuration and sentinel mode
- Volatile instruction rearrangement and why instruction rearrangement is prohibited
- Resnet+attention project complete code learning
- CVPR 2022 | single step 3D target recognizer based on sparse transformer
- Flume common commands and basic operations
- 【云原生】Nacos中的事件发布与订阅--观察者模式
- Pytoch uses torchnet Classerrormeter in meter
- Distributed solution - distributed session consistency problem
- RHCSA7
猜你喜欢

HiEngine:可媲美本地的云原生内存数据库引擎

你的下一台电脑何必是电脑,探索不一样的远程操作

Preliminary exploration of basic knowledge of MySQL

SAP UI5 ObjectPageLayout 控件使用方法分享

SAP UI5 视图里的 OverflowToolbar 控件

Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface

RHCSA5

SAP self-development records user login logs and other information

SAP UI5 DynamicPage 控件介绍

Add a new cloud disk to Huawei virtual machine
随机推荐
Compilation principle reading notes (1/12)
What if wechat is mistakenly sealed? Explain the underlying logic of wechat seal in detail
stirring! 2022 open atom global open source summit registration is hot!
2021-12-22 transaction record
Pytorch two-layer loop to realize the segmentation of large pictures
奔跑,开路
UNIX socket advanced learning diary - advanced i/o functions
Docker configures redis and redis clusters
Neural network of PRML reading notes (1)
10 minute fitness method reading notes (1/5)
研究:数据安全工具在 60% 的情况下无法抵御勒索软件
Taobao order amount check error, avoid capital loss API
Transactions from December 29, 2021 to January 4, 2022
Insmod prompt invalid module format
Pinduoduo flag insertion remarks API
激动人心!2022开放原子全球开源峰会报名火热开启!
SAP SEGW 事物码里的 ABAP Editor
jxl笔记
自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
10 minute fitness method reading notes (5/5)