SoFunction
Updated on 2025-04-12

Logical deletion function and example analysis in MyBatis-Plus

Logical deletion function and examples in MyBatis-Plus

introduce:

  • In actual development, data deletion operation is a common requirement.
  • MyBatis-Plus provides convenient and flexible logical deletion functions that simplify developers' operations when deleting data.

Case background

Suppose we have a blog system that contains the Blog class as the blog entity. In this system, we hope to be able to soft-delete the blog, and mark the blog to be deleted as deleted, rather than physically deleted directly.

Through logical deletion, we can keep deletion records, which helps track data change history and achieve audit requirements.

Use logical deletion

Configure database and entity classes

First, before using the logical deletion function, we need to make relevant configurations.

  1. In the database table, we need to add a field to represent the logical deletion status. For example, we can add a name calleddeletedoftinyintType field, default value is 0.
  2. In entity classes (here is the Blog class), we use Lombok to reduce the writing of getter and setter methods.

Here is an example:

import ;
import ;
import ;

@Data
@TableName("blog")
public class Blog {
    private Long id;
    private String title;
    private String content;
    @TableLogic
    private Integer deleted;
}

In the above example, we use@DataAnnotations simplify writing of getter and setter methods and use@TableNameThe annotation marks the database table name corresponding to the entity class. At the same time, use@TableLogicAnnotation markeddeletedFields are logical delete fields.

Perform a logical deletion operation

Next, we will demonstrate how to use the logical deletion function in MyBatis-Plus.

Method 1: Local configuration

Local configuration is suitable for situations where logical deletion is only required to be applied in a certain Mapper interface.

First, add it in the Mapper interface@RepositoryAnnotation and inheritanceBaseMapperInterface, for example:

import ;

@Repository
public interface BlogMapper extends BaseMapper<Blog> {
}

Then, a logically deleted call is made in the implementation class of the Service layer:

import ;
import ;

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Override
    public boolean deleteBlogById(Long id) {
        return (id) > 0;
    }
}

In the above example, we directly call the deleteById method of baseMapper for logical deletion operation. MyBatis-Plus will automatically set the logical deletion field to 1 (indicates that it has been deleted).

Method 2: Global configuration

Global configuration is suitable for situations where logical deletion is applied to all Mapper interfaces throughout the project.

First, configure global properties in or in the file:

mybatis-plus:
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

Then, set up the global configuration items and use them in the Mapper interface@TableLogicannotation:

import ;
import ;
import ;
import ;
import ;
import ;

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // Add optimistic lock plugin        (new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

In the above example, we created a @Configuration class and registered the paging plugin provided by Mybatis-Plus (PaginationInterceptor) and optimistic lock plug-in (OptimisticLockerInnerInterceptor). These plugins can be configured according to specific requirements.

test

To verify that the logical deletion function is working properly, we can write unit tests.

Here is a simple test example:

import ;
import ;
import ;

@SpringBootTest
public class BlogServiceTest {

    @Autowired
    private BlogService blogService;

    @Test
    public void testDeleteBlog() {
        Long blogId = 1L; // Suppose you want to delete a blog with ID 1        boolean result = (blogId);
        ("Delete successful: " + result);
    }
}

In the above test, we injected the BlogService interface and calleddeleteBlogByIdMethod to perform logical deletion operations.

By writing and running these test cases, we can verify normality when using logical deletion.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.