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

里面只有一条记录(ID=1),我们用update更新一把ID=1的这条记录。另外,最新 MySQL 面试题整理好了,大家可以在Java面试库小程序在线刷题。

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

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

结论:不要使用update语句的影响行数做重要的业务判断!


别用 System... 计时了,StopWatch 好用到爆!
关注Java技术栈看更多干货


获取 Spring Boot 实战笔记!
边栏推荐
- Find the original array for the inverse logarithm
- RestTemplate使用
- Comment imprimer le tableau original
- ESP32 ESP-IDF GPIO按键中断响应
- Terminology description in the field of software engineering
- 在支付宝上买基金安全吗?哪里可以买基金
- 解决无法读取META-INF.services里面定义的类
- Free trial of self-developed software noisecreater1.1
- Three minutes to quickly understand the whole process of website development
- 如何通过cdn方式使用阿里巴巴矢量图字体文件
猜你喜欢

MySQL learning

Notes on probability theory

Idea easy to use plug-in summary!!!

Solve the problem that the class defined in meta-inf.services cannot be read

Product learning (I) - structure diagram
![[unity shader custom material panel part II]](/img/d1/8632ae680299a27b7431b2d6e03fd3.png)
[unity shader custom material panel part II]

Software engineering review

Esp32 - ULP coprocessor reading Hall sensor in low power mode

ESP32 ESP-IDF GPIO按键中断响应

VS2019如何永久配置本地OpenCV4.5.5使用
随机推荐
How to use SCI hub
Summary of wechat official account embedded program to jump to wechat
[unity shader ablation effect _ case sharing]
关于变量是否线程安全的问题
JSON module
产品学习(三)——需求列表
Find the original array for the inverse logarithm
rclone 访问web界面
Esp32 - ULP coprocessor reading Hall sensor in low power mode
第五章 輸入/輸出(I/O)管理
H5 web page determines whether an app is installed. If it is installed, it will jump to the summary of the scheme to download if it is not installed
Grain Mall - environment (p1-p27)
Several ways of gson's @jsonadapter annotation
Problem: officeexception: failed to start and connect (III)
Problem: officeexception: failed to start and connect (II)
考研目录链接
mysql学习
Docker 安装部署Redis
MySQL learning
Some pits designed by NOC