介绍
@TableLogic
注解用于实现数据库的逻辑删除功能,它会影响
insert
、
select
、
update
和
delete
操作。当在字段上添加此注解并执行
BaseMapper
的
delete
方法时,实际执行的是更新操作,将删除字段设为特定值。
注解有两个参数:
value
(默认值)和
delval
(删除后的值)。在查询和更新时,会自动添加条件过滤已删除的数据。
@TableLogic注解
@TableLogic
:表示逻辑删除注解
效果:在字段上加上这个注解再执行
BaseMapper
的删除方法时,删除方法就会变成修改
例:
实体类:
@TableLogic
private Integer del;
service层:
调用BaseMapper的deleteById(id);
执行是效果:
加@TableLogic的情况下
走 Update 表名 set 加注解的列=值 where del=值
不加@TableLogic的情况下
走 delete from 表名 where del=值
@TableLogic注解参数
value = "" 默认的原值
delval = "" 删除后的值
@TableLogic(value="原值",delval="改值")
2. 注解说明
@TableLogic
:用于实现数据库数据逻辑删除
注意,该注解只对自动注入的 sql 起效
3. @TableLogic 对于 CIUD 的限制
3.1 插入(insert)
不作限制
3.2 查找(select)
@TableLogic
注解将会在 select 语句的 where 条件添加条件,过滤掉已删除数据
且使用
wrapper.entity
生成的 where 条件会忽略该字段
SELECT` `user_id,``name``,sex,age,deleted ``FROM` `user` `WHERE` `user_id=1 ``AND` `deleted=``'0'
3.3 更新(update)
@TableLogic
注解将会在 update 语句的 where 条件后追加条件,防止更新到已删除数据
且使用
wrapper.entity
生成的 where条件会忽略该字段
update` `user` `set` `deleted=1 ``where` `id = 1 ``and` `deleted=0
3.4 删除(delete)
@TableLogic
注解会将 delete 语句转变为 update 语句
update` `user` `set` `deleted=1 ``where` `id = 1 ``and` `deleted=0
4. @TableLogic 字段类型支持说明:
支持所有数据类型(推荐使用
Integer
、
Boolean
、
LocalDateTime
)
如果数据库字段使用
datetime
,逻辑未删除值和已删除值支持配置为字符串 null,另一个值支持配置为函数来获取值如now()
附录:
-
逻辑删除是为了方便数据恢复和保护数据本身价值等等的一种方案,但实际就是删除。
-
如果你需要频繁查出来看就不应使用逻辑删除,而是以一个状态去表示。
5. 属性说明
5.1 value
用来指定逻辑未删除值,默认为空字符串
5.2 delval
用来指定逻辑删除值,默认为空字符串。
6. 在配置文件中实现
当然,你也可以不在
@TableLogic
注解中指定
value
和
delval
属性的值。使用全局逻辑删除配置信息,配置如下:
# application.yml
mybatis-plus:
global-config:
db-config:
# 全局逻辑删除的实体字段名 (since 3.3.0, 配置后可以忽略 @TableLogic 中的配置)
logic-delete-field: flag
# 逻辑已删除值(默认为 1)
logic-delete-value: 1
# 逻辑未删除值(默认为 0)
logic-not-delete-value: 0
7. 代码实践
7.1 代码实践过程
我们在
user
数据表中添加一个
deleted
字段。
如果该字段值为1,表示记录被删除。如果该字段值为0,表示记录未被删除
-
向 user 数据表添加 deleted 字段,sql 如下:
-- 添加一个 deleted 字段,实现逻辑删除
ALTER TABLE `user`
ADD COLUMN `deleted` varchar(1) NULL DEFAULT 0 COMMENT '是否删除(1-删除;0-未删除)';
-
创建 user 表的实体类
AnnotationUser7Bean
使用
@TableLogic
注解将
deleted
成员变量指定为逻辑删除字段
import com.baomidou.mybatisplus.annotation.*;
@TableName(value = "user")
public class AnnotationUser7Bean {
@TableId(value = "user_id", type = IdType.AUTO)
private int userId;
@TableField("name")
private String name;
@TableField("sex")
private String sex;
@TableField("age")
private Integer age;
@TableLogic(value = "0", delval = "1")
private String deleted;
// 忽略 getter 和 setter 方法
@Override
public String toString() {
return "UserBean{" +
"userId=" + userId +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", deleted=" + deleted +
'}';
}
}
上面代码中,使用
@TableLogic
注解将
deleted
成员变量指定为逻辑删除字段
@TableLogic(value = "0", delval = "1")
private String deleted;
-
如果存在,则删除该用户信息
然后,查询用户ID小于10的用户信息
package com.hxstrive.mybatis_plus.simple_mapper.annotation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.mapper.AnnotationUser7Mapper;
import com.hxstrive.mybatis_plus.model.AnnotationUser7Bean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class AnnotationDemo7 {
@Autowired