当前位置:网站首页>MySQL 巨坑:update 更新慎用影响行数做判断!!!
MySQL 巨坑:update 更新慎用影响行数做判断!!!
2022-07-05 15:03: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.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
- qt creater断点调试程序详解
- I spring and autumn blasting-2
- 复现Thinkphp 2.x 任意代码执行漏洞
- Talking about how dataset and dataloader call when loading data__ getitem__ () function
- lv_font_conv离线转换
- Can I pass the PMP Exam in 20 days?
- How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
- Stm32+bh1750 photosensitive sensor obtains light intensity
- Fr exercise topic - simple question
- Hongmeng system -- Analysis from the perspective of business
猜你喜欢
Fr exercise topic - simple question
Bugku's eyes are not real
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
qt creater断点调试程序详解
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
Misc Basic test method and knowledge points of CTF
Crud of MySQL
Select sort and bubble sort
Thymeleaf uses background custom tool classes to process text
Bugku's Eval
随机推荐
Bugku easy_ nbt
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
华为哈勃化身硬科技IPO收割机
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
mapper. Comments in XML files
I spring and autumn blasting-1
如何将 DevSecOps 引入企业?
Mongdb learning notes
Install PHP extension spoole
数据库学习——数据库安全性
JMeter performance test: serveragent resource monitoring
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
复现Thinkphp 2.x 任意代码执行漏洞
sql server char nchar varchar和nvarchar的区别
Reasons and solutions for redis cache penetration and cache avalanche
Crud de MySQL
Interpretation of Apache linkage parameters in computing middleware
爱可可AI前沿推介(7.5)
Thymeleaf uses background custom tool classes to process text