baomidou/mybatis-plus · error · MybatisPlusException

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

Error message

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

What it means

IllegalSQLInnerInterceptor from the jsqlparser-5.0 module: after unwrapping ParenthesedExpressionList (the jsqlparser 5.x replacement for Parenthesis), a top-level OrExpression in the WHERE clause is rejected. Same anti-OR policy as the 4.9 build, adapted to the 5.x AST.

Source

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

        List<Join> joins = delete.getJoins();
        validWhere(where, table, (Connection) obj);
        validJoins(joins, table, (Connection) obj);
    }

    /**
     * 验证expression对象是不是 or、not等等
     *
     * @param expression ignore
     */
    private void validExpression(Expression expression) {
        while (expression instanceof ParenthesedExpressionList) {
            ParenthesedExpressionList<Expression> parenthesis = (ParenthesedExpressionList) expression;
            expression = parenthesis.get(0);
        }
        //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) {

View on GitHub (pinned to bf67d90747)

Solutions

  1. Replace OR with IN/BETWEEN/range predicates or split the query.
  2. Exclude the statement from the policy with @InterceptorIgnore(illegalSql = "true").
  3. Drop IllegalSQLInnerInterceptor from the chain if OR with proper indexes is acceptable to the team.

Example fix

// before
wrapper.eq(User::getName, "a").or().eq(User::getId, 1);

// after
wrapper.and(w -> w.eq(User::getId, 1).eq(User::getName, "a")); // indexed first, no OR
Defensive patterns

Strategy: validation

Validate before calling

// Same static guard as 4.9: forbid top-level OR before runtime
// ArchUnit-style rule banning QueryWrapper#or, plus XML scan for " OR " at WHERE top level
// git grep -niE "where.+\bor\b" -- src/main/resources/mapper

Try / catch

try {
    mapper.selectList(wrapper);
} catch (MybatisPlusException e) {
    if (String.valueOf(e.getMessage()).contains("不能使用【or】")) {
        log.error("policy violation: remove OR: {}", e.getMessage());
    }
}

Prevention

When it happens

Trigger: Executing a statement whose WHERE has a top-level OR (queryWrapper.or(...), .or wrappers, or native SQL) while the 5.0 IllegalSQLInnerInterceptor is registered in the MybatisPlusInterceptor chain.

Common situations: Adopting the 5.0 jsqlparser support module and keeping the strict SQL policy; developers adding OR conditions in wrappers; parentheses no longer hide the OR because ParenthesedExpressionList is unwrapped to its first element.

Related errors


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