当前位置:网站首页>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.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
- 上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
- Resnet+attention project complete code learning
- Detailed structure and code of inception V3
- Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
- How to connect the API interface of Taobao open platform (super detailed)
- 自然语言处理系列(一)入门概述
- 我在滴滴做开源
- 实战模拟│JWT 登录认证
- Setting up sqli lab environment
- Halcon 模板匹配实战代码(一)
猜你喜欢

Distance measuring sensor chip 4530a used in home intelligent lighting

【云原生】Nacos中的事件发布与订阅--观察者模式

A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this

Simply take stock reading notes (4/8)
![[Nacos cloud native] the first step of reading the source code is to start Nacos locally](/img/f8/d9b848593cf7380a6c99ee0a8158f8.png)
[Nacos cloud native] the first step of reading the source code is to start Nacos locally

Preliminary exploration of basic knowledge of MySQL

谈谈我写作生涯的画图技巧

SAP SEGW 事物码里的 ABAP Editor

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

激动人心!2022开放原子全球开源峰会报名火热开启!
随机推荐
stm32和电机开发(从架构图到文档编写)
石臻臻的2021总结和2022展望 | 文末彩蛋
JDBC -- extract JDBC tool classes
自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
Taobao flag insertion remarks | logistics delivery interface
Super efficient! The secret of swagger Yapi
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
Transactions on December 23, 2021
VoneDAO破解组织发展效能难题
【云原生】Nacos中的事件发布与订阅--观察者模式
View and modify the MySQL data storage directory under centos7
Notes for preparation of information system project manager --- information knowledge
ActiveMQ installation and deployment simple configuration (personal test)
Halcon 模板匹配实战代码(一)
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
SAP UI5 DynamicPage 控件介绍
RHCSA4
JDBC exercise - query data encapsulated into object return & simple login demo
SAP UI5 视图里的 OverflowToolbar 控件
How to connect the API interface of Taobao open platform (super detailed)