删除操作
# 物理删除
@Test
public void testDeleteById() {
int count = userMapper.deleteById(1463512055645134850L);
System.out.println(count);
}
1
2
3
4
5
2
3
4
5
# 批量删除
@Test
public void testDeleteBatchIds() {
int count = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
System.out.println(count);
}
1
2
3
4
5
2
3
4
5
# 条件删除
@Test
public void testDeleteByMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "stt");
map.put("age", 22);
int result = userMapper.deleteByMap(map);
System.out.println(result);
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 逻辑删除
非真正删除,添加一个标识位,标识该记录被删除
修改表,添加deleted字段
alter table user
add deleted boolean default false not null;
1
2
2
# 添加注解
- 修改User类添加deleted,并添加注解
@TableLogic
private Integer deleted;
1
2
2
# 配置 可选
- 在application.properties 中配置默认值,如果你的默认值和mybatis plus默认的一样,可以不配置
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
1
2
2
# 测试
- 进行删除,发现deleted标识位变化,再查询观察是否有被删除的记录出现
Last Updated: 2022/01/16, 11:29:51