当前位置: 首页 > web >正文

【MyBatis-Plus】禁用某个方法里面查询语句的逻辑删除标记

为了解决Mybatis-plus里面有逻辑删除的标识,但是其中某些查询方法不需要过滤删除的数据,需要全部查询而实现的功能。

1、定义注解

import java.lang.annotation.*;/*** DisableLogicDelete** @author behappyto* @since 2025/4/30 9:38*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DisableLogicDelete {/*** 逻辑删除字段名称** @return 逻辑删除字段名称*/String[] value() default "del_flag";
}

2、定义切面


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Component;
import org.zkdn.common.annotation.DisableLogicDelete;
import org.zkdn.common.interceptor.LogicDeleteInterceptor;/*** DisableLogicDeleteAspect** @author behappyto* @since 2025/4/30 9:39*/
@Slf4j
@Aspect
@Component
public class DisableLogicDeleteAspect {/*** 切面** @param joinPoint          连接点* @param disableLogicDelete 注解* @return Object* @throws Throwable Throwable*/@Around(value = "@annotation(disableLogicDelete)")public Object around(ProceedingJoinPoint joinPoint, DisableLogicDelete disableLogicDelete) throws Throwable {try {LogicDeleteInterceptor.logicDelete.set(Pair.of(disableLogicDelete.value(), false));return joinPoint.proceed();} finally {LogicDeleteInterceptor.logicDelete.remove();}}
}

3、定义拦截逻辑


import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Component;import java.sql.Connection;
import java.util.Properties;/*** LogicDeleteInterceptor** @author behappyto* @since 2025/4/30 9:42*/
@Slf4j
@Component
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class LogicDeleteInterceptor implements Interceptor {private final static String WHERE_DEL_REG_PATTERN = "\\s*=\\s*\\S+\\s*(and|AND)*";/*** 逻辑删除*/public static ThreadLocal<Pair<String[], Boolean>> logicDelete = new ThreadLocal<>();@Overridepublic Object intercept(Invocation invocation) throws Throwable {if (!Boolean.FALSE.equals(logicDelete.get().getSecond())) {return invocation.proceed();}StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());MetaObject metaObject = SystemMetaObject.forObject(statementHandler);MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {return invocation.proceed();}BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");String sql = boundSql.getSql();log.info("old sql:{}", sql);String[] fields = logicDelete.get().getFirst();for (String field : fields) {String delFlag = field + WHERE_DEL_REG_PATTERN;sql = ReUtil.replaceAll(sql, delFlag, "");}metaObject.setValue("delegate.boundSql.sql", sql);log.info("new sql:{}", sql);return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Interceptor.super.plugin(target);}@Overridepublic void setProperties(Properties properties) {Interceptor.super.setProperties(properties);}
}

4、使用方式

在需要禁用逻辑删除的方法上面加上注解 @DisableLogicDelete,可以自定义过滤的字段

http://www.xdnf.cn/news/3233.html

相关文章:

  • Java 中的 设计模式详解
  • errorno 和WSAGetlasterror的区别
  • 潇洒郎: 100% 成功搭建Docker私有镜像仓库并管理、删除镜像
  • Pytorch深度学习框架60天进阶学习计划 - 第55天:3D视觉基础(一)
  • 【人工智能】释放本地AI潜能:LM Studio用户脚本自动化DeepSeek的实战指南
  • (A2A Agent通信故障诊断体系)
  • Spring Boot 数据库最佳实践:从自动配置到高性能优化
  • 腾讯云CodeBuddy初体验
  • 从边缘到云端:边缘计算与云计算的协同未来
  • OpenCV-Python (官方)中文教程(部分一)_Day20
  • Elastic Security 8.18 和 9.0 中的新功能
  • Vue 3 动态组件
  • PostgreSQL可串行化快照隔离和冻结处理
  • 农产品园区展示系统——仙盟创梦IDE开发
  • 《PyTorch documentation》(PyTorch 文档)
  • vscode 个性化
  • 通过API网关防御重放攻击:技术方案与实战验证
  • 规划权重和全局优化器逻辑处理
  • 基于C++的IOT网关和平台5:github项目ctGateway开发指南
  • MyBatis的SQL映射文件中,`#`和`$`符号的区别
  • 大数据治理自动化与智能化实践指南:架构、工具与实战方案(含代码)
  • Oracle Bigfile 与 Smallfile 表空间对比分析
  • 打印机脱机状态原因有哪些?打印机脱机状态恢复正常解决方法
  • openEuler 22.03 安装 Nginx,支持离线安装
  • 基于Spring Boot + Vue 项目中引入deepseek方法
  • PostgreSQL事务与并发清理
  • 阿里云服务迁移实战: 06-切换DNS
  • scGPT-spatial:持续预训练scGPT用于空间转录组
  • 【diffusers 进阶之 PEFT 入门(五)】LoRA 权重如何接着训练?踩坑总结和解决方案
  • 在宝塔面板中安装OpenJDK-17的三种方法