MyBatis-Plus更新部分字段
UpdateWrapper修改指定的列
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("id","1").set("name", "tom"); Integer rows = userMapper.update(null, updateWrapper);
LambdaUpdateWrapper
LambdaUpdateWrapper<OilUser> updateWrapper= new LambdaUpdateWrapper<>(); updateWrapper.set(OilUser::getUsername, user.getUsername()); updateWrapper.set(OilUser::getTel, user.getTel()); updateWrapper.set(OilUser::getEmail, user.getEmail()); updateWrapper.eq(OilUser::getId, user.getId()); userMapper.update(null, updateWrapper); LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>(); lambdaUpdateWrapper.eq(User::getId, user.getId()).set(User::getAge, 34); Integer rows = userMapper.update(null, lambdaUpdateWrapper);
注意:
xxxMapper.update(null, updateWrapper);
第一个参数需要设置null,这样就只会更新set的字段
LambdaUpdateChainWrapper
LambdaUpdateChainWrapper<User> lambdaUpdateChainWrapper = new LambdaUpdateChainWrapper<>(userMapper); boolean update = lambdaUpdateChainWrapper.eq(User::getRealName, "tom").set(User::getAge, 33).update(); //分步写 /* lambdaUpdateChainWrapper.eq(User::getRealName, "tom"); if (user.getAge == 33) { lambdaUpdateChainWrapper.set(User::getAge, 33); } lambdaUpdateChainWrapper.update(); */