Mybatis Plus 逻辑删除,性能分析插件

2020-02-25  本文已影响0人  孙宏志

删除:
物理删除:从数据库中删除
逻辑删除:在数据库中还存在,但是查询中,查不到(本质:查询的时候多了一个判断)

1. 逻辑删除

数据库表需新增一个字段

deleted   default  0
// 1 逻辑已删除
// 0 逻辑未删除

配置application.properties 文件

# 全局逻辑删除字段
mybatis-plus.global-config.db-config.logic-delete-field=flag
# 逻辑已删除值
mybatis-plus.global-config.db-config.logic-delete-value=1
# 逻辑未删除值
mybatis-plus.global-config.db-config.logic-not-delete-value=0

在MybatisPlusConfig中新增bean(3.1.1以下)

MybatisPlus 3.1.1以下版本需要,以上不需要

@Bean
public ISqlInjector sqlInjector(){
    return new LogicSqlInjector();
}

在实体类字段上加上@TableLogic注解

@TableLogic
private Integer deleted;

测试类检查

@Test
void testDeleteUserTableLogic(){
    userMapper.deleteById(15L);
}

检查结果

select * from id_info_2 where deleted=1;

2. 性能分析插件

MybatisPlusConfig中加入(3.2.0及以下版本)

@Bean
@Profile({"dev","test"})
public PerformanceIntercepter performanceIntercepter(){
    return new PerformanceIntercepter();
}

需先设置环境: 如在Application.properties 文件添加

spring.profiles.active=dev
上一篇 下一篇

猜你喜欢

热点阅读