大家好,又见面了,我是你们的朋友全栈君。
开发系统时,有时候在实现功能时,删除操作需要实现逻辑删除,所谓欧吉删除就是将数据标记为删除,而并非真正的物理删除(非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)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137218.html原文链接:https://javaforall.cn