MyBatisPlus--逻辑删除

逻辑删除

开发系统时,有时候在实现功能时,删除操作需要实现逻辑删除,所谓欧吉删除就是将数据标记为删除,而并非真正的物理删除(非DELETE操作),查询时需要携带状态条件,确保被标记的数据不被查询,这样做的目的就是避免数据被真正的删除。

配置

application.properties

# 删除状态值为1
mybatis-plus.global-config.db-config.logic-delete-value=1
# 未删除状态为0
mybatis-plus.global-config.db-config.logic-not-delete-value=0

表结构

在这里插入图片描述

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User extends Model<User> {

    //指定主键为自增
    @TableId(type = IdType.AUTO)
    private Long id;

    //指定数据表中字段名
    @TableField(value = "user_name")
    private String userName;

    @TableField(fill = FieldFill.INSERT)
    private String password;
    private String name;
    private Integer age;
    private String email;

    //逻辑删除字段: 0,未删除; 1,已删除
    @TableLogic //标记为逻辑删除字段
    private Integer deleted;


    //指定该字段在表中是不存在的
    @TableField(exist = false)
    private String address;
}

测试

	@Test
    public void testDeleteById(){
        int rows = userMapper.deleteById(2l);
        System.out.println(rows);
    }

在这里插入图片描述

再次查询所有:

	@Test
    public void testSelectList(){
        List<User> users = userMapper.selectList(null);
        for (User u : users)
            System.out.println(u);
    }

控制台打印:

 Time:6 ms - ID:psers.zhang.demo.mapper.UserMapper.selectList
Execute SQL:
    SELECT
        id,
        user_name,
        password,
        name,
        age,
        email,
        deleted 
    FROM
        tb_user 
    WHERE
        deleted=0

User(id=4, userName=zhaoliu, password=123456, name=赵六, age=48, email=test4@qq.com, deleted=0, address=null)
User(id=5, userName=小强, password=88888, name=xiaoqiang, age=30, email=123456@qq.com, deleted=0, address=null)
发布了750 篇原创文章 · 获赞 2115 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/cold___play/article/details/104277041