MyBatis-Plus Advanced Function: Implementing Logical Deletion

I. Introduction

Previously, the editor talked about the use of MP from introduction to core functions. In the next few days, the editor will record some commonly used advanced functions of MP in practical projects.

Advanced functions are divided into: logical deletion, automatic filling, optimistic lock plug-in, performance analysis plug-in, multi-tenant SQL parser, dynamic table name SQL parser, SQL injector.

Today, let's talk about how to use logical deletion in MP. The logical deletion subordinate is not introduced too much. As the name implies, it means that the data has been deleted by means of logical judgment.

II. Specific Realization

Deletion function is a common requirement in projects, such as deleting a commodity or an order, but it often does not delete the actual data, but chooses logical deletion to achieve.

Take the user as an example. There is a is_delete field in the user table to indicate whether the user has been deleted or not. 0 indicates that the user has not been deleted and 1 indicates that the user has been deleted.

Step 1: First, we need to configure the undeleted and deleted status codes. The yml format is used here.

mybatis-plus:
  global-config:
    db-config:
      # 1 represents deleted, no default configuration is 1, you can also modify the configuration
      logic-delete-value: 1
      # 0 represents not deleted, no configuration defaults to 0, you can also modify the configuration
      logic-not-delete-value: 0

Step 2: Add an annotation @TableLogic to the field corresponding to the entity class.

/**
 * <p>
 * user
 * </p>
 *
 * @author IT Cheap man
 * @since 2019-06-14
 */
@Data
@TableName("sys_user")
public class User extends Model<User> implements Serializable {

    private static final long serialVersionUID=1L;

    /**
     * Primary key id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    // Other fields are omitted

    /**
     * Delete
     */
    @TableLogic
    private String isDelete;

}

Step 3: Test the test. Now when we test deleting a user by ID, we can see that the execution of the SQL statement is UPDATE.

    @Test
    public void delete() {
        int i = userMapper.deleteById(1);
        System.out.println(i);

    }
DEBUG==>  Preparing: UPDATE sys_user SET is_delete='1' WHERE id=? AND is_delete='0' 
DEBUG==> Parameters: 1(Integer)
DEBUG<==    Updates: 1

3. Attention to Details in Use

Details 1: When we use the function of MP logical deletion, such as query execution and modification method, MP will automatically add the condition of not deleted for us. Except for custom sql

    @Test
    public void select(){
        List<User> users = userMapper.selectList(Wrappers.<User>lambdaQuery().eq(User::getAge, 18));
        users.forEach(System.out::println);
    }

    @Test
    public void update(){
        User user = new User();
        user.setEmail("Test@email.com");
        
        int update = userMapper.update(user, Wrappers.<User>lambdaQuery().eq(User::getAge, 18));
        System.out.println(update);
    }
DEBUG==>  Preparing: SELECT id,login_name,name,password,email,salt,sex,age,phone,user_type,status,organization_id,create_time,is_delete FROM sys_user WHERE is_delete='0' AND age = ? 
DEBUG==> Parameters: 18(Integer)
DEBUG<==      Total: 0


DEBUG==>  Preparing: UPDATE sys_user SET email=? WHERE is_delete='0' AND age = ? 
DEBUG==> Parameters: Test@email.com(String), 18(Integer)
DEBUG<==    Updates: 0

Detail 2: When we query the data, we don't want to query whether the field with the logo is deleted or not. We can solve it quickly as follows. Of course, you can also use the select expression in Wrapper to exclude some query fields, but it is more convenient by annotation.

    /**
     * Delete
    */
    @TableLogic
    @TableField(select = false)
    private String isDelete;

 

Keywords: SQL Mybatis

Added by nev25 on Tue, 17 Sep 2019 12:33:25 +0300