如何绕过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_ = "";
        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("删除失败");
}

你可能感兴趣的:(springboot,spring,boot,后端,mybatis)