Skip to content

逻辑删除

说明

逻辑删除注解,让删除变成修改,就是伪删除;
注意:DeleteChain操作删除,逻辑删除不会生效!!!

1 属性 beforeValue

未删除前的值,只能是固定值;时间类型的逻辑,可不填

2 属性 afterValue

删除后的值,可固定值或者动态值 例如 afterValue = "{NOW}",目前支持LocalDateTime,Date,Long,Integer,框架自动给值

3 属性 deleteTimeField

逻辑删除的时间字段,可不填,填了系统自动update 时 设置删除时间,deleteTimeField对应的字段类型 支持:LocalDateTime,Date,Long,Integer类型

4 全局开关(默认开)

java
MybatisMpConfig.setLogicDeleteSwitch(true);

5 局部开关

java
try (LogicDeleteSwitch ignored = LogicDeleteSwitch.with(false)) {
    logicDeleteTestMapper.getById(1);
}

上面代码必须try (LogicDeleteSwitch ignored = LogicDeleteSwitch.with(false))

下面的更简单:

java
LogicDeleteTest logicDeleteTest = LogicDeleteUtil.execute(false, () -> {
    return logicDeleteTestMapper.getById(1);
});

示例

实体类

java
@Data
@Table
public class SysUser {

    @TableId
    private Integer id;

    private String userName;

    private String password;
    
    private Integer roleId;

    private LocalDateTime createTime;

    private LocalDateTime deleteTime;

    @LogicDelete(beforeValue = "0", afterValue = "1", deleteTimeField = "deleteTime")
    private Byte deleted; 
}

Mapper

继承 MybatisMapper 即可

java
public interface SysUserMapper extends MybatisMapper<SysUser> {

}

如何使用

java
@Service
public class Test{

    @Autowired
    private SysUserMapper sysUserMapper;

    public void demo() {
        sysUserMapper.deleteById(1);
    }
}

自动把delete 改成 update 语句;前往@LogicDelete 查看