MyBatis-Plus逻辑删除

Laughing
2021-04-22 / 0 评论 / 2,782 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年03月18日,已超过341天没有更新,若内容或图片失效,请留言反馈。

逻辑删除

::: tip 说明:
只对自动注入的sql起效:

  • 插入: 不作限制
  • 查找: 追加where条件过滤掉已删除数据,且使用 wrapper.entity 生成的where条件会忽略该字段
  • 更新: 追加where条件防止更新到已删除数据,且使用 wrapper.entity 生成的where条件会忽略该字段
  • 删除: 转变为 更新

例如:

  • 删除: update user set deleted=1 where id = 1 and deleted=0
  • 查找: select id,name,deleted from user where deleted=0

字段类型支持说明:

  • 支持所有数据类型(推荐使用 Integer,Boolean,LocalDateTime)
  • 如果数据库字段使用datetime,逻辑未删除值和已删除值支持配置为字符串null,另一个值支持配置为函数来获取值如now()

附录:

  • 逻辑删除是为了方便数据恢复和保护数据本身价值等等的一种方案,但实际就是删除。
  • 如果你需要频繁查出来看就不应使用逻辑删除,而是以一个状态去表示。

使用方法:

步骤1: MySql表结构

create table if not exists user
(
   id bigint not null comment '主键ID'
      primary key,
   name varchar(30) null comment '姓名',
   age int null comment '年龄',
   email varchar(50) null comment '邮箱',
   del_flag char default 'N' null
);

步骤2: 预置数据

INSERT INTO user (id, name, age, email, del_flag) VALUES (1, 'Jone', 18, 'test1@baomidou.com', 'Y');
INSERT INTO user (id, name, age, email, del_flag) VALUES (2, 'Jack', 20, 'test2@baomidou.com', 'N');
INSERT INTO user (id, name, age, email, del_flag) VALUES (3, 'Tom', 28, 'test3@baomidou.com', 'N');
INSERT INTO user (id, name, age, email, del_flag) VALUES (4, 'Sandy', 21, 'test4@baomidou.com', 'N');
INSERT INTO user (id, name, age, email, del_flag) VALUES (5, 'Billie', 24, 'test5@baomidou.com', 'N');

步骤3: 配置com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig

  • 例: application.yml
mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: del_flag  # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
      logic-delete-value: 'Y' # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 'N' # 逻辑未删除值(默认为 0)

步骤4: 实体类字段上加上@TableLogic注解

@Data
@TableName(value = "`user`")
public class User implements Serializable {
    /**
     * 主键ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 姓名
     */
    @TableField(value = "`name`")
    private String name;

    /**
     * 年龄
     */
    @TableField(value = "age")
    private Integer age;

    /**
     * 邮箱
     */
    @TableField(value = "email")
    private String email;

    /**
     * 逻辑删除字段
     */
    @TableField(value = "del_flag")
    @TableLogic
    private String delFlag;

    private static final long serialVersionUID = 1L;

    public static final String COL_ID = "id";

    public static final String COL_NAME = "name";

    public static final String COL_AGE = "age";

    public static final String COL_EMAIL = "email";
}

步骤4: 测试

@GetMapping("logicdelete/{id}")
public Boolean logicdelete(@PathVariable("id") Long id){
    return userService.removeById(id);
}

常见问题:

1. 如何 insert ?

  1. 字段在数据库定义默认值(推荐)
  2. insert 前自己 set 值
  3. 使用自动填充功能

2. 删除接口自动填充功能失效

  1. 使用 update 方法并: UpdateWrapper.set(column, value)(推荐)
  2. 使用 update 方法并: UpdateWrapper.setSql("column=value")
  3. 使用Sql注入器注入com.baomidou.mybatisplus.extension.injector.methods.LogicDeleteByIdWithFill并使用(推荐)
1

评论 (0)

取消
  1. 头像
    234234234
    Windows 10 · QQ Browser

    不错不错

    回复