mybatis-plus 逻辑删除失效问题

473 阅读1分钟

实体bean

在实体字段上添加注解

@TableLogic private Integer logicDelete;


@Data
@EqualsAndHashCode(callSuper = true)
@TableName("t_device_batch_task")
@BelongTenantTable(tableName="t_device_batch_task")
public class DeviceBatchTask extends BasePO {

    private static final long serialVersionUID = 1L;

    /**
     * 申请设备数
     */
    private Integer applyDeviceNum;

    /**
     * 已添加设备数
     */
    private Integer finishDeviceNum;

    /**
     * 产品key
     */
    private String productKey;

    /**
     * 产品id
     */
    private Long productId;

    /**
     * 状态<字典:APPLY_STATUS>
     */
    private String status;

    /**
     * 文件路径
     */
    private String filePath;

    /**
     * 批次添加方式<字典:DEVICE_ADD_MODE>
     */
    private String mode;

    /**
     * 租户号;默认
     */
    private Integer tenantId;

    /**
     * 乐观锁
     */
    @Version
    private Integer lastVersion;

    /**
     * 是否删除;1删除0正常
     */
    @TableLogic
    private Integer logicDelete;


}

解决办法

使用deleteById 方法

使用 deleteById 方法(推荐)

使用 update 方法

使用 update 方法并: UpdateWrapper.set(column, value)(推荐)

public BaseResp removeDevice(DeviceRequest request) {
    LambdaUpdateWrapper<Device> updateWrapper = new LambdaUpdateWrapper();
    updateWrapper.eq(Device::getId, request.getId());
    //利用此方法进行更新删除
    updateWrapper.set(Device::getLogicDelete, LogicDeleteEnum.DELETED.getLogicDelete());
    Device device = new Device();
    HermesFillUtil.fillUpdate(device);    
    //update(device, updateWrapper)=>entity   实体对象 (set 条件值,可以为 null)
    //update(device, updateWrapper)=>实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
    deviceRepository.getBaseMapper().update(device, updateWrapper);
    return BaseResp.success(ResultCode.SUCCESS.getMessage());
}