• Mybatis 框架 ( 六 ) 逻辑删除@TableLogic


    4.7.@TableLogic逻辑删除

    4.7.1.@TableLogic注解说明

    @TableLogic:表示逻辑删除注解

    效果:在字段上加上这个注解再执行BaseMapper的删除方法 (如 : deleteById(id); ) 时,删除方法就会变成修改

    加@TableLogic的情况下 : 走 Update 表名 set 加注解的列=值 where del=值
    不加@TableLogic的情况下 : 走 delete from 表名 where del=值

    4.7.2.注解说明

    @TableLogic注解参数

    value = “” 默认的原值 , 用来指定逻辑未删除值,默认为空字符串
     delval = “” 删除后的值 , 用来指定逻辑删除值,默认为空字符串。
     @TableLogic(value=“原值”,delval=“改值”)

       @TableLogic(value = "0", delval = "1")
       private String deleted;
    
    • 1
    • 2

    @TableLogic 用于实现数据库数据逻辑删除

    注意,该注解只对自动注入的 sql 起效

    4.7.3.@TableLogic 对于 CRUD 的限制

    4.7.3.1 插入(insert)

    不作限制

    4.7.3.2 查找(select)

    @TableLogic 注解将会在 select 语句的 where 条件添加条件,过滤掉已删除数据

    且使用 wrapper.entity 生成的 where 条件会忽略该字段

    SELECT user_id, name,sex,age,deleted FROM user WHERE user_id=1 AND deleted = '0'
    
    • 1
    4.7.3.3 更新(update)

    @TableLogic 注解将会在 update 语句的 where 条件后追加条件,防止更新到已删除数据

    且使用 wrapper.entity 生成的 where条件会忽略该字段

    update user set age = 13 where user_id=1  AND deleted = '0'
    
    • 1
    4.7.3.4 删除(delete)

    @TableLogic 注解会将 delete 语句转变为 update 语句

    update  user set deleted = '1'  where user_id=1  AND deleted = '0'
    
    • 1

    4.7.4.@TableLogic 字段类型支持说明:

    支持所有数据类型(推荐使用 Integer、Boolean、LocalDateTime)

    如果数据库字段使用 datetime,逻辑未删除值和已删除值支持配置为字符串 null,另一个值支持配置为函数来获取值如now()

    4.7.5.在配置文件中实现

    当然,你也可以不在 @TableLogic 注解中指定 value 和 delval 属性的值。使用全局逻辑删除配置信息,配置如下:

    application.yml

    mybatis-plus:
      global-config:
        db-config:
    	  # 全局逻辑删除的实体字段名 (since 3.3.0, 配置后可以忽略 @TableLogic 中的配置)
          logic-delete-field: flag
          # 逻辑已删除值(默认为 1)
          logic-delete-value: 1
          # 逻辑未删除值(默认为 0)
          logic-not-delete-value: 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    Python学习三(面向对象)
    OOP设计原则详解
    一款GoFrame+Vue+ElementUI后台管理框架
    微信小程序组件仿某音
    AI全栈大模型工程师(七)内容审核
    NIO基础知识
    上半年住户存款增加10.33万亿元,平均每天约571亿存款涌向银行
    java反射全面复习
    聊聊Qt中的Widget调色板QPalette
    性能优化:Netty连接参数优化
  • 原文地址:https://blog.csdn.net/yuanchun05/article/details/132824533