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
相关推荐
小贤plus13 小时前
SpringBoot 三大核心注解精讲:@ControllerAdvice、@RestControllerAdvice、@Validated 分组校验(实战)
java
吠品13 小时前
Java byte数组与String互转:编码细节与踩坑记录
java·linux·服务器
W_3260013 小时前
Python文件进阶:一维数据与 CSV 文件读写
开发语言·python
晚风醉蝶13 小时前
1-6-插入排序-InsertionSort
java·数据结构·排序算法
Tyler_TXZ13 小时前
C++C语言之——二叉树
c语言·开发语言·数据结构·c++·二叉树
不会代码的小猴14 小时前
C++新增关键字
开发语言·c++·笔记
yaoxin52112314 小时前
497. Java 反射 - 使用反射读取注解
java·开发语言·python
2019一路前行14 小时前
Python 函数式编程
开发语言·python
eralong15 小时前
Java 面向对象:继承、多态、接口
java·后端
哦虎!15 小时前
【数据库】事务
java·数据库·mysql