mybatis更新字段从有值变为null

mybatisplus更新时,如果传入的值为null,对于update来说是不更新当前字段,此时对于原本有值的数据更新为null时更新不了
解决办法:

LambdaUpdateWrapper wrapper = null;
for (ResourceToHangDto resourceToHangDto : list) {
			Resource resource = new Resource();
			//对于mybaits自带的数据库操作方法只需要将version放进要修改的实体类where条件和set的数值会自动发生改变
			resource.setVersion(resourceToHangDto.getVersion().longValue());
			resource.setStockTotalWeight(resourceToHangDto.getStockTotalWeight());
			resource.setItemPrice(resourceToHangDto.getItemPrice());
			resource.setAccountType(resourceToHangDto.getAccountType());
			wrapper = new LambdaUpdateWrapper ();
			wrapper.eq(Resource::getResourceCode, resourceToHangDto.getResourceCode());
			if (resourceToHangDto.getStockQuantity() == null){
				//库存数量为空时将库存数量更新为null
				wrapper.set(Resource::getStockQuantity, null);
			}else {
				resource.setStockQuantity(resourceToHangDto.getStockQuantity());
			}
			num += baseMapper.update(resource, wrapper);
		}

mybatis更新字段从有值变为null_第1张图片

你可能感兴趣的:(java)