How to implement logical deletion in MybatisPlus?
MybatisPlus can implement logical deletion through the following steps:
- Flag for deletion
- TableLogic
@TableLogic
private Integer deleteFlag;
- configuration file for an application
- properties file for the application
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleteFlag # 配置逻辑删除字段
logic-delete-value: 1 # 配置逻辑删除的值
logic-not-delete-value: 0 # 配置未删除的值
- Mapping base
- a designated storage location
@Repository
public interface UserMapper extends BaseMapper<User> {
}
- removeById
@Autowired
private UserMapper userMapper;
public void deleteUserById(Long id) {
userMapper.deleteById(id);
}
Please note that logical deletion does not actually remove data from the database, but instead updates the value of the deleteFlag field to the configured logical deletion value.