In-depth analysis of the logical deletion function and examples in MyBatis-Plus

In-depth analysis of the logical deletion function and examples in MyBatis-Plus

Introduction:
In actual development, data deletion is a common requirement. MyBatis-Plus provides a convenient and flexible logic delete function, which can simplify the operation of developers when deleting data. This article will explain in detail the logical deletion feature in MyBatis-Plus, and demonstrate and explain it in combination with actual cases.

case background

Suppose we have a blog system that contains the Blog class as a blog entity. In this system, we want to be able to perform soft delete operations on blogs, that is, to mark the blogs to be deleted as deleted, instead of directly deleting them physically. Through logical deletion, we can keep deletion records, which is helpful for tracking data change history and fulfilling auditing requirements.

Use the tombstone function

Configure database and entity classes

First, before using the tombstone function, we need to configure it.

  1. In the database table, we need to add a field to represent the status of the tombstone. For example, we can add a type field called deleted, tinyintwith a default value of 0.
  2. In the entity class (here the Blog class), we use Lombok to reduce the effort of writing getter and setter methods. Here is an example:
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@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 to simplify the writing of getter and setter methods, and use @TableNameannotations to mark the database table names corresponding to entity classes. At the same time, the fields @TableLogicare marked deletedas tombstone fields using annotations.

Execute tombstone operation

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

Method 1: Local configuration

Partial configuration is suitable for the case where only the tombstone function needs to be applied in a certain Mapper interface.

First, add annotations to the Mapper interface @Repositoryand inherit BaseMapperthe interface, for example:

import org.springframework.stereotype.Repository;

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

Then, call the tombstone in the implementation class of the Service layer:

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

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

In the above example, we directly call the deleteById method of baseMapper to perform logical deletion. MyBatis-Plus will automatically set the tombstone field to 1 (meaning deleted).

Method 2: Global configuration

The global configuration is suitable for the case where the tombstone function is applied to all Mapper interfaces in the entire project.

First, configure global properties in the application.yml or application.properties file:

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

Then, set global configuration items and use annotations in the Mapper interface @TableLogic:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {
    
    

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加乐观锁插件
        interceptor.addInnerInterceptor(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 ( PaginationInterceptor) and optimistic lock plugin ( OptimisticLockerInnerInterceptor) provided by Mybatis-Plus. These plugins can be configured according to specific needs.

test

To verify that the tombstone functionality is working, we can write unit tests. Here is a simple test example:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BlogServiceTest {
    
    

    @Autowired
    private BlogService blogService;

    @Test
    public void testDeleteBlog() {
    
    
        Long blogId = 1L; // 假设要删除ID为1的博客
        boolean result = blogService.deleteBlogById(blogId);
        System.out.println("Delete successful: " + result);
    }
}

In the above test, we inject the BlogService interface and call deleteBlogByIdthe method to perform the tombstone operation.

By writing and running these test cases, we can verify the normality when using the tombstone feature.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132031797