本文代码样例均已上传至Gitee:https://gitee.com/tqbx/springboot-samples-learn
只对自动注入的sql起效:
注:
逻辑删除是为了方便数据恢复和保护数据本身价值等等的一种方案,但实际就是删除。
如果你需要频繁查出来看就不应使用逻辑删除,而是以一个状态去表示。
logic-delete-field
后可以忽略不配置步骤2。mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
// 逻辑删除字段
@TableLogic
private Integer deleted;
@Test
void logicDelete(){
//UPDATE user SET deleted=1 WHERE deleted=0 AND (name = ?)
boolean remove = userService.remove(
new QueryWrapper<User>().eq("name", "summer"));
System.out.println(remove);
}
@Test
void select(){
//SELECT id,name,deleted FROM user WHERE deleted=0
List<User> list = userService.list(
new QueryWrapper<User>().select("id","name","deleted"));
System.out.println(list);
}
@Test
public void updateById(){
//UPDATE user SET age=? WHERE id=? AND deleted=0
User user = new User();
user.setId(1320008348799713282L);
user.setAge(20);
boolean b = userService.updateById(user);
System.out.println(b);
}
使用注解标识需要排除的字段:
@TableField(select = false)
private Integer deleted;
原文:https://www.cnblogs.com/summerday152/p/13874936.html