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

如何绕过myabtis-plus的逻辑删除条件

目标

mp中所有方法都会带上逻辑删除,如果启用了逻辑删除,有时候我们需要忽略逻辑删除.改如何实现

解决方法

  • 自定义DeleteReal 方法
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author Administrator
 */
public class DeleteReal extends AbstractMethod {
    /**
     * @param methodName 方法名
     * @since 3.5.0
     */
    public DeleteReal() {
        super("deleteReal");
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlMethod sqlMethod = SqlMethod.DELETE;
        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
                sqlWhereEntityWrapper(true, tableInfo),
                sqlComment());
        SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass);
        return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
    }

    @Override
    protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
        /*
         * Wrapper SQL
         */
        String _sgEs_ = "<bind name=\"_sgEs_\" value=\"ew.sqlSegment != null and ew.sqlSegment != ''\"/>";
        String andSqlSegment = SqlScriptUtils.convertIf(String.format(" AND ${%s}", WRAPPER_SQLSEGMENT), String.format("_sgEs_ and %s", WRAPPER_NONEMPTYOFNORMAL), true);
        String lastSqlSegment = SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT), String.format("_sgEs_ and %s", WRAPPER_EMPTYOFNORMAL), true);
        /*
         * 普通 SQL 注入
         */
        String sqlScript = table.getAllSqlWhere(false, false, true, WRAPPER_ENTITY_DOT);
        sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true);
        sqlScript = SqlScriptUtils.convertWhere(sqlScript + NEWLINE + andSqlSegment) + NEWLINE + lastSqlSegment;
        sqlScript = SqlScriptUtils.convertIf(_sgEs_ + NEWLINE + sqlScript, String.format("%s != null", WRAPPER), true);
        return newLine ? NEWLINE + sqlScript : sqlScript;
    }
}

  • 注入
public class MySqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
        methodList.add(new SelectRealList());
        methodList.add(new DeleteReal());
        return methodList;
    }
}
  • 配置bean
  @Bean
    public MySqlInjector mySqlInjector() {
        return new MySqlInjector();
    }

修改basemapper

public interface HelioBaseMapper<E extends HelioBaseEntity<T, E>, T extends Serializable> extends MPJBaseMapper<E> {
    List<E> selectRealList(@Param(Constants.WRAPPER) Wrapper<E> queryWrapper);

    int deleteReal(@Param(Constants.WRAPPER) Wrapper<E> queryWrapper);
}

使用方法

int demoKey = sysConfigMapper.deleteReal(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getConfigKey, "demoKey"));
if (demoKey > 0) {
    log.info("删除成功");
} else {
    log.info("删除失败");
}

相关文章:

  • Unity URP管线与HDRP管线对比
  • Mysql 数据库编程技术01
  • Linux上位机开发实践(硬件设计的创新)
  • SQL Server 2022 数据同步到 Elasticsearch 思考
  • Spring 核心技术解析【纯干货版】- XV:Spring 网络模块 Spring-Web 模块精讲
  • 16.1Linux自带的LED灯驱动实验(知识)_csdn
  • [3.2] 深入了解recv参数列表里的status
  • 信息系统项目管理中各个知识领域的概要描述及其管理流程
  • 【网络安全】 防火墙技术
  • 卢瑟福实验室2025.4.5
  • java流程控制05:Switch选择结构
  • Python开发GUI 框架TKinter、PyQt、PySide、wxPython、Kivy、PyGTK
  • Day 7
  • 采掘队绩效考核制度与生产管理
  • Angular 2 模板语法详解
  • Ubuntu(CentOS、Rockylinux等)快速进入深度学习pytorch环境
  • 【Redis】背景知识
  • 信息系统项目管理师-第十章-项目进度管理
  • 2-Visual Studio 2022 NET开发Windows桌面软件并连接SQL Server数据库
  • 【前端】【tailwind】「由浅入深」 的方式,从基础概念到高级应用,逐步解析 Tailwind CSS 的分层系统,并设置自己的原子类和组件类