baomidou/mybatis-plus · error · MybatisPlusException

非法SQL,where条件中不能使用【!=】关键字,错误!=信息:{}

Error message

非法SQL,where条件中不能使用【!=】关键字,错误!=信息:{}

What it means

IllegalSQLInnerInterceptor (jsqlparser 4.9) throws when the WHERE condition's expression (after unwrapping parentheses) is a NotEqualsTo node — i.e. the SQL uses the != / <> operator. This mirrors the same anti-index-bypass policy that forbids OR; the offending expression text is appended.

Source

Thrown at mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java:179

    }

    /**
     * 验证expression对象是不是 or、not等等
     *
     * @param expression ignore
     */
    private void validExpression(Expression expression) {
        while (expression instanceof Parenthesis) {
            Parenthesis parenthesis = (Parenthesis) expression;
            expression = parenthesis.getExpression();
        }
        //where条件使用了 or 关键字
        if (expression instanceof OrExpression) {
            OrExpression orExpression = (OrExpression) expression;
            throw new MybatisPlusException("非法SQL,where条件中不能使用【or】关键字,错误or信息:" + orExpression.toString());
        } else if (expression instanceof NotEqualsTo) {
            NotEqualsTo notEqualsTo = (NotEqualsTo) expression;
            throw new MybatisPlusException("非法SQL,where条件中不能使用【!=】关键字,错误!=信息:" + notEqualsTo.toString());
        } else if (expression instanceof BinaryExpression) {
            BinaryExpression binaryExpression = (BinaryExpression) expression;
            // TODO 升级 jsqlparser 后待实现
//            if (binaryExpression.isNot()) {
//                throw new MybatisPlusException("非法SQL,where条件中不能使用【not】关键字,错误not信息:" + binaryExpression.toString());
//            }
            if (binaryExpression.getLeftExpression() instanceof Function) {
                Function function = (Function) binaryExpression.getLeftExpression();
                throw new MybatisPlusException("非法SQL,where条件中不能使用数据库函数,错误函数信息:" + function.toString());
            }
            if (binaryExpression.getRightExpression() instanceof Subtraction) {
                Subtraction subSelect = (Subtraction) binaryExpression.getRightExpression();
                throw new MybatisPlusException("非法SQL,where条件中不能使用子查询,错误子查询SQL信息:" + subSelect.toString());
            }
        } else if (expression instanceof InExpression) {
            InExpression inExpression = (InExpression) expression;
            if (inExpression.getRightExpression() instanceof Subtraction) {
                Subtraction subSelect = (Subtraction) inExpression.getRightExpression();

View on GitHub (pinned to bf67d90747)

Solutions

  1. Rewrite != as a positive range/IN predicate where possible (e.g. status != 0 becomes status IN (1,2)).
  2. Annotate the mapper method with @InterceptorIgnore(illegalSql = "true") to opt the specific statement out.
  3. If the policy is too strict for the project, remove IllegalSQLInnerInterceptor from the MybatisPlusInterceptor chain.

Example fix

// before
wrapper.ne(User::getStatus, 0);

// after
wrapper.in(User::getStatus, 1, 2);
Defensive patterns

Strategy: validation

Validate before calling

// Grep-level guard before release: forbid .ne( and '<>' / '!=' in mapped SQL
// CI example: git grep -nE "\.ne\\(|!=|<>" -- src/main/resources/mapper && exit 1

Try / catch

try {
    mapper.selectList(wrapper);
} catch (MybatisPlusException e) {
    if (String.valueOf(e.getMessage()).contains("不能使用【!=】")) {
        log.error("policy violation: replace != with IN/eq: {}", e.getMessage());
    }
}

Prevention

When it happens

Trigger: Executing SQL or a QueryWrapper-built statement with a 'column != value' predicate (e.g. .ne(...) producing status != 0) while IllegalSQLInnerInterceptor is registered.

Common situations: Developers using wrapper.ne(...) or hand-written '<>' predicates; the check fires even for logically selective != conditions because the policy is syntax-based, not cost-based.

Related errors


AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14). Data as JSON: /api/errors/b3652c1591b5884a. Report an issue: GitHub.