当前位置:网站首页>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.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
- DVWA range clearance tutorial
- The difference between abstract classes and interfaces in PHP (PHP interview theory question)
- Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
- 华为哈勃化身硬科技IPO收割机
- Easyocr character recognition
- 1330:【例8.3】最少步数
- Common PHP interview questions (1) (written PHP interview questions)
- Can gbase 8A view the location of SQL statement history?
- lvgl 显示图片示例
- Cartoon: programmers don't repair computers!
猜你喜欢
随机推荐
How to solve the problem of garbled code when installing dependency through NPM or yarn
go学习 ------jwt的相关知识
Severlet learning foundation
漫画:程序员不是修电脑的!
queryRunner. Query method
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
当代人的水焦虑:好水究竟在哪里?
easyOCR 字符識別
Super wow fast row, you are worth learning!
I spring and autumn blasting-2
Calculate weight and comprehensive score by R entropy weight method
Bugku telnet
爱可可AI前沿推介(7.5)
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
Leetcode: Shortest Word Distance II
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
Usage and usage instructions of JDBC connection pool
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
Thymeleaf uses background custom tool classes to process text