MybatisPlus使用LambdaQueryWrapper更新时 int默认值问题

问题:

java 复制代码
User user = new User();
        user.setBalance(1000);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username","Jack");
        userMapper.update(user, queryWrapper);

通过用户名,更新金额,使用queryWrapper,出现了多更新其他字段的问题。

原因

User实体类中,存在int属性,int属性具有默认值0,在执行userMapper.update(user, queryWrapper);

语句时,user中除了我们set的Balance外,还存在一个int字段status的默认值是0,也会被update语句扫描加载并执行

powershell 复制代码
 ==>  Preparing: UPDATE user SET status=?, balance=? WHERE (username = ?)
 ==> Parameters: 0(Integer), 1000(Integer), Jack(String)

解决方案

将User实体类中的int属性改为Integer

java 复制代码
//    private int status;
//    private int balance;
    private Integer status;
    private Integer balance;

问题分析

int:

  • int是32位 有符号的以二进制补码表示的整数
  • 最小值是-2147483648(-2^31)
  • 最大值是 2147483647(2^31-1)
  • 一般的整形变量默认为int类型
  • 默认值是0

Integer

Integer类包含四种常量:

  • MAX_VALUE:值为2^31-1的常量,表示int类型能够表示的最大值
  • MIN_VALUE:值为-2^31的常量,表示int类型能够表示的最小值
  • SIZE:用来以二进制补码形式表示int值的比特位数
  • TYPE:表示基本类型int的class实例

总结

  • int类型是基本数据类型,Integer是引用数据类型
  • Integer是int类型的包装类,int的初始值是0,Integer的初始值是null
相关推荐
benchmark_cc3 小时前
如何用 Python + QuantDash 快速构建高胜率“配对交易(Pairs Trading)”策略?
开发语言·人工智能·python·pandas·量化交易·quantdash
阿维的博客日记3 小时前
MultipartFile 是不是表示仅仅是一个分片?
java·后端·spring·multipartfile
程序员无隅4 小时前
Coding Agent 为什么压缩上下文后还能继续工作?上下文模块设计拆解
java·开发语言·数据库
Python+994 小时前
Java 枚举类(Enum)详解:从基础到高级应用
java·开发语言·python
二炮手亮子4 小时前
浅记java线程池
java·开发语言
一路向北North4 小时前
Spring Security OAuth2.0(23):分布式系统授权-转发明文给微服务
java·spring·微服务
zmzb01035 小时前
C++课后习题训练记录Day157
开发语言·c++
dunge20265 小时前
2026年7月最新ChatGPT Plus / Pro 与 Codex:当 AI Agent 最新5.6版本来袭,必须理解事务、幂等与补偿
开发语言·人工智能·python
爱吃牛肉的大老虎6 小时前
rust基础之环境搭建
java·开发语言·rust
openKylin6 小时前
与全球技术演进同频,openKylin 3.0从C迈向Rust
c语言·开发语言·rust·开源·开放原子·openkylin