逻辑删除
-
yml
mybatis-plus:
# Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)
mapper-locations: classpath*:/mapper/*.xml
global-config:
db-config:
property-format: "\"%s\""
#### 配置逻辑删除
logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
configuration: # 控制台打印日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 启动类
@SpringBootApplication
@MapperScan("org.example.demo08.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 实体类,作为逻辑删除的字段添加注解@TableLogic
@Data
@Accessors(chain = true)
public class Common {
private Long id;
private String name;
@TableLogic
private Integer deleted;
}
@Data
@Accessors(chain = true)
public class Null1 {
private Long id;
private String name;
@TableLogic(delval = "null", value = "1")
private Integer deleted;
}
@Data
@Accessors(chain = true)
public class Null2 {
private Long id;
private String name;
@TableLogic(delval = "now()", value = "null")
private LocalDateTime delTime;
}
- 测试1
@Test
public void tCommon() {
// 插入数据
List<Long> ids = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Common common = new Common().setName("" + i);
commonMapper.insert(common);
ids.add(common.getId());
}
log.error("------------------------------------------------deleteById--------------------------------------------------------");
commonMapper.deleteById(ids.remove(0));
log.error("------------------------------------------------deleteByMap--------------------------------------------------------");
commonMapper.deleteByMap(Maps.newHashMap("id", ids.remove(1)));
log.error("------------------------------------------------delete--------------------------------------------------------");
commonMapper.delete(Wrappers.<Common>query().eq("id", ids.remove(2)));
log.error("------------------------------------------------deleteBatchIds--------------------------------------------------------");
commonMapper.deleteBatchIds(Arrays.asList(ids.remove(0), ids.remove(0)));
log.error("------------------------------------------------updateById--------------------------------------------------------");
commonMapper.updateById(new Common().setId(ids.remove(0)).setName("老王"));
log.error("------------------------------------------------update--------------------------------------------------------");
commonMapper.update(new Common().setName("老王"), Wrappers.<Common>update().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectById--------------------------------------------------------");
commonMapper.selectById(ids.remove(0));
log.error("------------------------------------------------selectBatchIds--------------------------------------------------------");
commonMapper.selectBatchIds(Arrays.asList(ids.remove(0), ids.remove(0)));
log.error("------------------------------------------------selectByMap--------------------------------------------------------");
commonMapper.selectByMap(Maps.newHashMap("id", ids.remove(0)));
log.error("------------------------------------------------selectOne--------------------------------------------------------");
commonMapper.selectOne(Wrappers.<Common>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectCount--------------------------------------------------------");
commonMapper.selectCount(Wrappers.<Common>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectList--------------------------------------------------------");
commonMapper.selectList(Wrappers.<Common>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectMaps--------------------------------------------------------");
commonMapper.selectMaps(Wrappers.<Common>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectObjs--------------------------------------------------------");
commonMapper.selectObjs(Wrappers.<Common>query().select("id").eq("id", ids.remove(0)));
log.error("------------------------------------------------selectPage--------------------------------------------------------");
commonMapper.selectPage(new Page<>(), Wrappers.<Common>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectMapsPage--------------------------------------------------------");
commonMapper.selectMapsPage(new Page<>(), Wrappers.<Common>query().eq("id", ids.remove(0)));
}
# 控制台打印如下
------------------------------------------------deleteById--------------------------------------------------------
UPDATE common SET deleted=1 WHERE id=? AND deleted=0
==> Parameters: 1549323343028195329(Long)
<== Updates: 1
------------------------------------------------deleteByMap--------------------------------------------------------
UPDATE common SET deleted=1 WHERE id = ? AND deleted=0
==> Parameters: 1549323349088964610(Long)
<== Updates: 1
------------------------------------------------delete--------------------------------------------------------
UPDATE common SET deleted=1 WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323349999128577(Long)
<== Updates: 1
------------------------------------------------deleteBatchIds--------------------------------------------------------
UPDATE common SET deleted=1 WHERE id IN ( ? , ? ) AND deleted=0
==> Parameters: 1549323348623396866(Long), 1549323349609058306(Long)
<== Updates: 2
------------------------------------------------updateById--------------------------------------------------------
UPDATE common SET name=? WHERE id=? AND deleted=0
==> Parameters: 老王(String), 1549323350519222273(Long)
<== Updates: 1
------------------------------------------------update--------------------------------------------------------
UPDATE common SET name=? WHERE deleted=0 AND (id = ?)
==> Parameters: 老王(String), 1549323351173533697(Long)
<== Updates: 1
------------------------------------------------selectById--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE id=? AND deleted=0
==> Parameters: 1549323351815262210(Long)
<== Columns: id, name, deleted
<== Row: 1549323351815262210, 7, 0
<== Total: 1
------------------------------------------------selectBatchIds--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE id IN ( ? , ? ) AND deleted=0
==> Parameters: 1549323352461185025(Long), 1549323353249714177(Long)
<== Columns: id, name, deleted
<== Row: 1549323352461185025, 8, 0
<== Row: 1549323353249714177, 9, 0
<== Total: 2
------------------------------------------------selectByMap--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE id = ? AND deleted=0
==> Parameters: 1549323354029854721(Long)
<== Columns: id, name, deleted
<== Row: 1549323354029854721, 10, 0
<== Total: 1
------------------------------------------------selectOne--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323354747080706(Long)
<== Columns: id, name, deleted
<== Row: 1549323354747080706, 11, 0
<== Total: 1
------------------------------------------------selectCount--------------------------------------------------------
SELECT COUNT( 1 ) FROM common WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323355393003522(Long)
<== Columns: COUNT( 1 )
<== Row: 1
<== Total: 1
------------------------------------------------selectList--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323355980206082(Long)
<== Columns: id, name, deleted
<== Row: 1549323355980206082, 13, 0
<== Total: 1
------------------------------------------------selectMaps--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323356638711809(Long)
<== Columns: id, name, deleted
<== Row: 1549323356638711809, 14, 0
<== Total: 1
------------------------------------------------selectObjs--------------------------------------------------------
SELECT id FROM common WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323357406269442(Long)
<== Columns: id
<== Row: 1549323357406269442
<== Total: 1
------------------------------------------------selectPage--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323358190604289(Long)
<== Columns: id, name, deleted
<== Row: 1549323358190604289, 16, 0
<== Total: 1
------------------------------------------------selectMapsPage--------------------------------------------------------
SELECT id,name,deleted FROM common WHERE deleted=0 AND (id = ?)
==> Parameters: 1549323358979133441(Long)
<== Columns: id, name, deleted
<== Row: 1549323358979133441, 17, 0
<== Total: 1
- 测试2
@Test
public void tNull1() {
List<Long> ids = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Null1 null1 = new Null1().setName("" + i).setDeleted(1);
null1Mapper.insert(null1);
ids.add(null1.getId());
}
log.error("------------------------------------------------deleteById--------------------------------------------------------");
null1Mapper.deleteById(ids.remove(0));
log.error("------------------------------------------------deleteByMap--------------------------------------------------------");
null1Mapper.deleteByMap(Maps.newHashMap("id", ids.remove(0)));
log.error("------------------------------------------------delete--------------------------------------------------------");
null1Mapper.delete(Wrappers.<Null1>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------deleteBatchIds--------------------------------------------------------");
null1Mapper.deleteBatchIds(Arrays.asList(ids.remove(0), ids.remove(0)));
log.error("------------------------------------------------updateById--------------------------------------------------------");
null1Mapper.updateById(new Null1().setId(ids.remove(0)).setName("老王"));
log.error("------------------------------------------------update--------------------------------------------------------");
null1Mapper.update(new Null1().setName("老王"), Wrappers.<Null1>update().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectById--------------------------------------------------------");
null1Mapper.selectById(ids.remove(0));
log.error("------------------------------------------------selectBatchIds--------------------------------------------------------");
null1Mapper.selectBatchIds(Arrays.asList(ids.remove(0), ids.remove(0)));
log.error("------------------------------------------------selectByMap--------------------------------------------------------");
null1Mapper.selectByMap(Maps.newHashMap("id", ids.remove(0)));
log.error("------------------------------------------------selectOne--------------------------------------------------------");
null1Mapper.selectOne(Wrappers.<Null1>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectCount--------------------------------------------------------");
null1Mapper.selectCount(Wrappers.<Null1>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectList--------------------------------------------------------");
null1Mapper.selectList(Wrappers.<Null1>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectMaps--------------------------------------------------------");
null1Mapper.selectMaps(Wrappers.<Null1>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectObjs--------------------------------------------------------");
null1Mapper.selectObjs(Wrappers.<Null1>query().select("id").eq("id", ids.remove(0)));
log.error("------------------------------------------------selectPage--------------------------------------------------------");
null1Mapper.selectPage(new Page<>(), Wrappers.<Null1>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectMapsPage--------------------------------------------------------");
null1Mapper.selectMapsPage(new Page<>(), Wrappers.<Null1>query().eq("id", ids.remove(0)));
}
# 控制台
------------------------------------------------deleteById--------------------------------------------------------
UPDATE null1 SET deleted=null WHERE id=? AND deleted=1
==> Parameters: 1549325620182917122(Long)
<== Updates: 1
------------------------------------------------deleteByMap--------------------------------------------------------
UPDATE null1 SET deleted=null WHERE id = ? AND deleted=1
==> Parameters: 1549325626038165506(Long)
<== Updates: 1
------------------------------------------------delete--------------------------------------------------------
UPDATE null1 SET deleted=null WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325626566647809(Long)
<== Updates: 1
------------------------------------------------deleteBatchIds--------------------------------------------------------
UPDATE null1 SET deleted=null WHERE id IN ( ? , ? ) AND deleted=1
==> Parameters: 1549325627090935809(Long), 1549325627481006081(Long)
<== Updates: 2
------------------------------------------------updateById--------------------------------------------------------
UPDATE null1 SET name=? WHERE id=? AND deleted=1
==> Parameters: 老王(String), 1549325628005294081(Long)
<== Updates: 1
------------------------------------------------update--------------------------------------------------------
UPDATE null1 SET name=? WHERE deleted=1 AND (id = ?)
==> Parameters: 老王(String), 1549325628592496641(Long)
<== Updates: 1
------------------------------------------------selectById--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE id=? AND deleted=1
==> Parameters: 1549325629439746050(Long)
<== Columns: id, name, deleted
<== Row: 1549325629439746050, 7, 1
<== Total: 1
------------------------------------------------selectBatchIds--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE id IN ( ? , ? ) AND deleted=1
==> Parameters: 1549325630094057473(Long), 1549325630677065730(Long)
<== Columns: id, name, deleted
<== Row: 1549325630094057473, 8, 1
<== Row: 1549325630677065730, 9, 1
<== Total: 2
------------------------------------------------selectByMap--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE id = ? AND deleted=1
==> Parameters: 1549325631339765761(Long)
<== Columns: id, name, deleted
<== Row: 1549325631339765761, 10, 1
<== Total: 1
------------------------------------------------selectOne--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325631994077185(Long)
<== Columns: id, name, deleted
<== Row: 1549325631994077185, 11, 1
<== Total: 1
------------------------------------------------selectCount--------------------------------------------------------
SELECT COUNT( 1 ) FROM null1 WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325632711303170(Long)
<== Columns: COUNT( 1 )
<== Row: 1
<== Total: 1
------------------------------------------------selectList--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325633361420289(Long)
<== Columns: id, name, deleted
<== Row: 1549325633361420289, 13, 1
<== Total: 1
------------------------------------------------selectMaps--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325634015731714(Long)
<== Columns: id, name, deleted
<== Row: 1549325634015731714, 14, 1
<== Total: 1
------------------------------------------------selectObjs--------------------------------------------------------
SELECT id FROM null1 WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325634670043138(Long)
<== Columns: id
<== Row: 1549325634670043138
<== Total: 1
------------------------------------------------selectPage--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325635324354562(Long)
<== Columns: id, name, deleted
<== Row: 1549325635324354562, 16, 1
<== Total: 1
------------------------------------------------selectMapsPage--------------------------------------------------------
SELECT id,name,deleted FROM null1 WHERE deleted=1 AND (id = ?)
==> Parameters: 1549325635911557122(Long)
<== Columns: id, name, deleted
<== Row: 1549325635911557122, 17, 1
<== Total: 1
- 测试3
@Test
public void tNull2() {
List<Long> ids = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Null2 null2 = new Null2().setName("" + i);
null2Mapper.insert(null2);
ids.add(null2.getId());
}
log.error("------------------------------------------------deleteById--------------------------------------------------------");
null2Mapper.deleteById(ids.remove(0));
log.error("------------------------------------------------deleteByMap--------------------------------------------------------");
null2Mapper.deleteByMap(Maps.newHashMap("id", ids.remove(0)));
log.error("------------------------------------------------delete--------------------------------------------------------");
null2Mapper.delete(Wrappers.<Null2>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------deleteBatchIds--------------------------------------------------------");
null2Mapper.deleteBatchIds(Arrays.asList(ids.remove(0), ids.remove(0)));
log.error("------------------------------------------------updateById--------------------------------------------------------");
null2Mapper.updateById(new Null2().setId(ids.remove(0)).setName("老王"));
log.error("------------------------------------------------update--------------------------------------------------------");
null2Mapper.update(new Null2().setName("老王"), Wrappers.<Null2>update().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectById--------------------------------------------------------");
null2Mapper.selectById(ids.remove(0));
log.error("------------------------------------------------selectBatchIds--------------------------------------------------------");
null2Mapper.selectBatchIds(Arrays.asList(ids.remove(0), ids.remove(0)));
log.error("------------------------------------------------selectByMap--------------------------------------------------------");
null2Mapper.selectByMap(Maps.newHashMap("id", ids.remove(0)));
log.error("------------------------------------------------selectOne--------------------------------------------------------");
null2Mapper.selectOne(Wrappers.<Null2>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectCount--------------------------------------------------------");
null2Mapper.selectCount(Wrappers.<Null2>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectList--------------------------------------------------------");
null2Mapper.selectList(Wrappers.<Null2>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectMaps--------------------------------------------------------");
null2Mapper.selectMaps(Wrappers.<Null2>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectObjs--------------------------------------------------------");
null2Mapper.selectObjs(Wrappers.<Null2>query().select("id").eq("id", ids.remove(0)));
log.error("------------------------------------------------selectPage--------------------------------------------------------");
null2Mapper.selectPage(new Page<>(), Wrappers.<Null2>query().eq("id", ids.remove(0)));
log.error("------------------------------------------------selectMapsPage--------------------------------------------------------");
null2Mapper.selectMapsPage(new Page<>(), Wrappers.<Null2>query().eq("id", ids.remove(0)));
}
# 控制台
------------------------------------------------deleteById--------------------------------------------------------
UPDATE null2 SET del_time=now() WHERE id=? AND del_time IS NULL
==> Parameters: 1549326832462692354(Long)
<== Updates: 0
------------------------------------------------deleteByMap--------------------------------------------------------
UPDATE null2 SET del_time=now() WHERE id = ? AND del_time IS NULL
==> Parameters: 1549326838401826818(Long)
<== Updates: 0
------------------------------------------------delete--------------------------------------------------------
UPDATE null2 SET del_time=now() WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326839056138241(Long)
<== Updates: 0
------------------------------------------------deleteBatchIds--------------------------------------------------------
UPDATE null2 SET del_time=now() WHERE id IN ( ? , ? ) AND del_time IS NULL
==> Parameters: 1549326839706255362(Long), 1549326840364761090(Long)
<== Updates: 0
------------------------------------------------updateById--------------------------------------------------------
UPDATE null2 SET name=? WHERE id=? AND del_time IS NULL
==> Parameters: 老王(String), 1549326841081987074(Long)
<== Updates: 0
------------------------------------------------update--------------------------------------------------------
UPDATE null2 SET name=? WHERE del_time IS NULL AND (id = ?)
==> Parameters: 老王(String), 1549326841799213058(Long)
<== Updates: 0
------------------------------------------------selectById--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE id=? AND del_time IS NULL
==> Parameters: 1549326842520633345(Long)
<== Total: 0
------------------------------------------------selectBatchIds--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE id IN ( ? , ? ) AND del_time IS NULL
==> Parameters: 1549326843107835906(Long), 1549326843757953026(Long)
<== Total: 0
------------------------------------------------selectByMap--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE id = ? AND del_time IS NULL
==> Parameters: 1549326844420653057(Long)
<== Total: 0
------------------------------------------------selectOne--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326845079158786(Long)
<== Total: 0
------------------------------------------------selectCount--------------------------------------------------------
SELECT COUNT( 1 ) FROM null2 WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326845729275905(Long)
<== Columns: COUNT( 1 )
<== Row: 0
<== Total: 1
------------------------------------------------selectList--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326846320672770(Long)
<== Total: 0
------------------------------------------------selectMaps--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326847042093057(Long)
<== Total: 0
------------------------------------------------selectObjs--------------------------------------------------------
SELECT id FROM null2 WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326847700598786(Long)
<== Total: 0
------------------------------------------------selectPage--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326848291995649(Long)
<== Total: 0
------------------------------------------------selectMapsPage--------------------------------------------------------
SELECT id,name,del_time FROM null2 WHERE del_time IS NULL AND (id = ?)
==> Parameters: 1549326848883392514(Long)
<== Total: 0