pom

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.0.6</version>
  5. </dependency>

application.yml

#mybatis
mybatis-plus:
  global-config:
    # 数据库相关配置
    db-config:
      #逻辑删除配置
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

config

package com.soyuan.bigdata.heartbeat.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tangwx@soyuan.com.cn
 * @date 2020/4/24 14:33
 */
@Configuration
public class MyBatisPlusConfiguration {

    /**
     * mybatis-plus 逻辑删除配置
     * @return
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
}

entity&&表结构

package com.soyuan.bigdata.heartbeat.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

/**
 * @author tangwx@soyuan.com.cn
 * @date 2020/4/28 14:56
 */
@Getter
@Setter
@TableName("project_info")
public class ProjectInfo {
    /**
     * 项目ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 逻辑删除标志-前端无需关心
     * @ignore
     */
    @TableField("logic_delete")
    @TableLogic
    private Integer logicDelete;

    ...
    省略其他字段    
    ... 
}

image.png

使用说明

当使用mybatis-plus自己封装的service或者mapper方法时,比如removeById 或者 select*时,会自动将逻辑删除位置为指定的值。(1或0)

效果: 使用mp自带方法删除和查找都会附带逻辑删除功能 (自己写的xml不会)

example
删除 update user set deleted=1 where id =1 and deleted=0
查找 select * from user where deleted=0


@Override
public Boolean delete(Serializable id) {
    return super.removeById(id);
}

更详细的使用说明请参考官方指导手册:https://mp.baomidou.com/guide/logic-delete.html