Usage of Mybatis-Plus logical deletion

Introduction

illustrate

This article introduces the usage of logical deletion in Mybatis-Plus.

The delete function is a requirement that is often seen in projects. For example, if you need to delete an order, generally the actual data will not be deleted, but logical deletion. There is an is_deleted field in the general table to identify whether the user has been deleted, 0 means not deleted, 1 means deleted.

official website

Tombstone | MyBatis-Plus​

configuration (not required)

Configure to delete undeleted status codes

Configure the status codes for undeleted and deleted respectively. The yml format is used here.

mybatis-plus:
  global-config:
    db-config:
      # 1 代表已删除。默认是1。也可写为字段名,如:id,这样删除时会将id的值写到逻辑删除字段
      logic-delete-value: 1
      # 0 代表未删除。默认是0
      logic-not-delete-value: 0

Global configuration field name (optional)

If the project code is more standardized, and the logical deletion fields of all tables use the same name (for example: deleted_flag), we can add the following unified configuration in application.properties, so that the corresponding entity class fields do not need to add @TableLogic annotations up:

Note: If the global tombstone field value is set and there is still @TableLogic on the entity class, the one on the entity shall prevail, ignoring the global.

mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: deleted_flag

entity

Annotate @TableLogic on the field corresponding to the entity class

@Data
@TableName("t_user")
public class User {
    
    
 
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
 
    @TableLogic
    private String deleted;
}

database

Database deleted column, set to tinyint type, length is 1

test

insert test

When inserting, the sql statement will not involve the logically deleted fields.

query, modify test

After using the MP tombstone function, execute the query and modification methods, and MP will automatically add undeleted conditions for us. Except 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("[email protected]");
        
        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 deleted='0' AND age = ? 
DEBUG==> Parameters: 18(Integer)
DEBUG<==      Total: 0
 
 
DEBUG==>  Preparing: UPDATE sys_user SET email=? WHERE deleted='0' AND age = ? 
DEBUG==> Parameters: Test@email.com(String), 18(Integer)
DEBUG<==    Updates: 0

delete test

(The executed 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 deleted='0' 
DEBUG==> Parameters: 1(Integer)
DEBUG<==    Updates: 1

Do not query the is_deleted field

@TableField(select = false) is enough. You can also use Wrapper's select expression to exclude certain query fields, but it is more convenient to use annotations.

    @TableLogic
    @TableField(select = false)
    private String deleted;

Guess you like

Origin blog.csdn.net/weixin_45444807/article/details/131528063