当前位置:网站首页>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.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
- RHCSA2
- 跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
- Four common problems of e-commerce sellers' refund and cash return, with solutions
- Pytoch through datasets Imagefolder loads datasets directly from files
- Simply take stock reading notes (2/8)
- Keras implements verification code identification
- Simply take stock reading notes (3/8)
- Full text search of MySQL
- From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code
- 自然语言处理系列(一)入门概述
猜你喜欢
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Annotation problem and hidden Markov model
What if wechat is mistakenly sealed? Explain the underlying logic of wechat seal in detail
初识Linkerd项目
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
Simply take stock reading notes (2/8)
Vonedao solves the problem of organizational development effectiveness
Pinduoduo flag insertion remarks API
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
随机推荐
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
SAP UI5 视图里的 OverflowToolbar 控件
奔跑,开路
Common commands and basic operations of Apache Phoenix
Docker configures redis and redis clusters
GNN(pytorch-geometric)
2021-12-22 transaction record
Preliminary exploration of basic knowledge of MySQL
10 minute fitness method reading notes (5/5)
Resnet18 actual battle Baoke dream spirit
C alarm design
Kotlin process control and circulation
Kotlin变量
About LDA model
单独编译内核模块
SAP SEGW 事物码里的 Association 建模方式
Didi open source Delta: AI developers can easily train natural language models
10 minute fitness method reading notes (3/5)
Using docker for MySQL 8.0 master-slave configuration
【Nacos云原生】阅读源码第一步,本地启动Nacos