当前位置:网站首页>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.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
猜你喜欢

Ctfshow web entry explosion

Misc Basic test method and knowledge points of CTF

爱可可AI前沿推介(7.5)

当代人的水焦虑:好水究竟在哪里?

Common redis data types and application scenarios

Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel

Bugku easy_ nbt

Creation and use of thymeleaf template
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps

Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
随机推荐
1330: [example 8.3] minimum steps
Interview shock 62: what are the precautions for group by?
Number protection AXB function! (essence)
I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
Detailed explanation of usememo, memo, useref and other relevant hooks
episodic和batch的定义
I include of spring and Autumn
Bugku's steganography
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Reasons and solutions for redis cache penetration and cache avalanche
Cartoon: what are the attributes of a good programmer?
ionic cordova项目修改插件
Your childhood happiness was contracted by it
STM32+BH1750光敏传感器获取光照强度
easyOCR 字符识别
Ctfshow web entry information collection
Mysql---- function
Leetcode: Shortest Word Distance II
[recruitment position] Software Engineer (full stack) - public safety direction