How can logical deletion be implemented in MyBatis Plus?

The logic deletion feature of MyBatis-Plus can be implemented by adding a logical deletion flag field in the entity class and configuring the logic deletion method in the Mapper interface.

Firstly, add a logical delete flag field to the entity class, for example:

public class User {
    private Long id;
    private String name;
    private Integer age;
    private Integer deleted; // 逻辑删除标识字段
    // 省略getter和setter方法
}

Next, configure the logical deletion method in the Mapper interface. You can use the @TableLogic annotation provided by MyBatis-Plus to identify the logical deletion field, for example:

public interface UserMapper extends BaseMapper<User> {
    @TableLogic
    int deleteById(Long id);
}

In the above configuration, the @TableLogic annotation is used to mark the field for logical deletion, and then in the deleteById method, the int data type is used as the return type to indicate the number of records deleted.

Finally, call the deleteById method in the place where logical deletion is being used, for example:

@Autowired
private UserMapper userMapper;

public void deleteUser(Long id) {
    userMapper.deleteById(id);
}

The above are the steps to implement logical deletion using MyBatis-Plus. By adding a logical deletion flag field and configuring the logical deletion method, you can easily achieve logical deletion functionality.

bannerAds