逻辑删除

开启配置

  1. 1、修改 集成 全局注入器为 LogicSqlInjector
  2. @Bean
  3. public ISqlInjector sqlInjector(){
  4. return new LogicSqlInjector();
  5. }
  6. 2、全局注入值:
  7. logicDeleteValue // 逻辑删除全局值
  8. logicNotDeleteValue // 逻辑未删除全局值
  9. 3、逻辑删除的字段需要注解 @TableLogic

Mybatis-Plus逻辑删除视频教程

全局配置注入LogicSqlInjector

Java Config方式:

  1. @Bean
  2. public GlobalConfiguration globalConfiguration() {
  3. GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector());
  4. conf.setLogicDeleteValue("-1");
  5. conf.setLogicNotDeleteValue("1");
  6. conf.setIdType(2);
  7. return conf;
  8. }

XML配置方式:

  1. <bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
  2. <property name="sqlInjector" ref="logicSqlInjector" />
  3. <property name="logicDeleteValue" value="-1" />
  4. <property name="logicNotDeleteValue" value="1" />
  5. <property name="idType" value="2" />
  6. </bean>
  7. <bean id="logicSqlInjector" class="com.baomidou.mybatisplus.mapper.LogicSqlInjector" />

逻辑删除实体

  1. @TableName("tbl_user")
  2. public class UserLogicDelete {
  3. private Long id;
  4. ...
  5. @TableField(value = "delete_flag")
  6. @TableLogic
  7. private Integer deleteFlag;
  8. }

逻辑删除效果

会在mp自带查询和更新方法的sql后面,追加『逻辑删除字段』=『LogicNotDeleteValue默认值』删除方法: deleteById()和其他delete方法, 底层SQL调用的是update tbl_xxx set 『逻辑删除字段』=『logicDeleteValue默认值』

Spring Boot mp-starter配置参考

application.yml

  1. mybatis-plus:
  2. mapper-locations: classpath:/mapper/*Mapper.xml
  3. #实体扫描,多个package用逗号或者分号分隔
  4. typeAliasesPackage: com.baomidou.springboot.entity
  5. typeEnumsPackage: com.baomidou.springboot.entity.enums
  6. global-config:
  7. #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
  8. id-type: 2
  9. #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
  10. field-strategy: 2
  11. db-column-underline: true
  12. #逻辑删除配置
  13. logic-delete-value: 0
  14. logic-not-delete-value: 1
  15. sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector