当前位置:网站首页>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.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
- Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
- Hongmeng system -- Analysis from the perspective of business
- R 熵权法计算权重及综合得分
- GPS原始坐标转百度地图坐标(纯C代码)
- Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
- Ecotone technology has passed ISO27001 and iso21434 safety management system certification
- mapper. Comments in XML files
- Common MySQL interview questions
- go学习 ------jwt的相关知识
- Redis distributed lock principle and its implementation with PHP (1)
猜你喜欢
Fr exercise topic --- comprehensive question
MySQL----函数
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
Detailed explanation of QT creator breakpoint debugger
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
Change multiple file names with one click
华为哈勃化身硬科技IPO收割机
Select sort and bubble sort
Talking about how dataset and dataloader call when loading data__ getitem__ () function
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
随机推荐
Reasons and solutions for redis cache penetration and cache avalanche
I spring web upload
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
Bugku telnet
Huiyuan, 30, is going to have a new owner
PHP high concurrency and large traffic solution (PHP interview theory question)
漫画:优秀的程序员具备哪些属性?
数据库学习——数据库安全性
sql server char nchar varchar和nvarchar的区别
Your childhood happiness was contracted by it
Common MySQL interview questions (1) (written MySQL interview questions)
Ctfshow web entry command execution
Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
机器学习笔记 - 灰狼优化
Ten billion massage machine blue ocean, difficult to be a giant
MySQL之CRUD
30岁汇源,要换新主人了
Anaconda uses China University of science and technology source
GPS原始坐标转百度地图坐标(纯C代码)
Can gbase 8A view the location of SQL statement history?