欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > 如何绕过myabtis-plus的逻辑删除条件

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

2025/4/6 11:11:10 来源:https://blog.csdn.net/u014087707/article/details/147015907  浏览:    关键词:如何绕过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");}@Overridepublic 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);}@Overrideprotected 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 {@Overridepublic 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
  @Beanpublic 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("删除失败");
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词