{"record":{"id":"b42e16befa2ff47a","repo":"baomidou/mybatis-plus","slug":"sql-where-b42e16","errorCode":null,"errorMessage":"非法SQL，where条件中不能使用【!=】关键字，错误!=信息：","messagePattern":"非法SQL，where条件中不能使用【!=】关键字，错误!=信息：","errorType":"exception","errorClass":"MybatisPlusException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java","lineNumber":178,"sourceCode":"    }\n\n    /**\n     * 验证expression对象是不是 or、not等等\n     *\n     * @param expression ignore\n     */\n    private void validExpression(Expression expression) {\n        while (expression instanceof ParenthesedExpressionList) {\n            ParenthesedExpressionList<Expression> parenthesis = (ParenthesedExpressionList) expression;\n            expression = parenthesis.get(0);\n        }\n        //where条件使用了 or 关键字\n        if (expression instanceof OrExpression) {\n            OrExpression orExpression = (OrExpression) expression;\n            throw new MybatisPlusException(\"非法SQL，where条件中不能使用【or】关键字，错误or信息：\" + orExpression.toString());\n        } else if (expression instanceof NotEqualsTo) {\n            NotEqualsTo notEqualsTo = (NotEqualsTo) expression;\n            throw new MybatisPlusException(\"非法SQL，where条件中不能使用【!=】关键字，错误!=信息：\" + notEqualsTo.toString());\n        } else if (expression instanceof BinaryExpression) {\n            BinaryExpression binaryExpression = (BinaryExpression) expression;\n            // TODO 升级 jsqlparser 后待实现\n//            if (binaryExpression.isNot()) {\n//                throw new MybatisPlusException(\"非法SQL，where条件中不能使用【not】关键字，错误not信息：\" + binaryExpression.toString());\n//            }\n            if (binaryExpression.getLeftExpression() instanceof Function) {\n                Function function = (Function) binaryExpression.getLeftExpression();\n                throw new MybatisPlusException(\"非法SQL，where条件中不能使用数据库函数，错误函数信息：\" + function.toString());\n            }\n            if (binaryExpression.getRightExpression() instanceof Subtraction) {\n                Subtraction subSelect = (Subtraction) binaryExpression.getRightExpression();\n                throw new MybatisPlusException(\"非法SQL，where条件中不能使用子查询，错误子查询SQL信息：\" + subSelect.toString());\n            }\n        } else if (expression instanceof InExpression) {\n            InExpression inExpression = (InExpression) expression;\n            if (inExpression.getRightExpression() instanceof Subtraction) {\n                Subtraction subSelect = (Subtraction) inExpression.getRightExpression();","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java#L160-L196","documentation":"Thrown by IllegalSQLInnerInterceptor.validExpression when the WHERE expression is a NotEqualsTo node. The interceptor's SQL standard forbids != (and <>) in WHERE because inequality comparisons generally cannot use a b-tree index efficiently; the offending expression is appended after '错误!=信息：'.","triggerScenarios":"Executing WHERE col != value or WHERE col <> value through a MybatisPlusInterceptor chain containing IllegalSQLInnerInterceptor; also QueryWrapper.ne(...) generated SQL.","commonSituations":"Filter-out logic (e.g. WHERE deleted != 1, WHERE status != 'CANCELLED') in existing mappers; teams adopting the interceptor later and discovering widespread .ne() usage in wrappers.","solutions":["Invert the condition into an explicit allow-list: WHERE deleted != 1 becomes WHERE deleted = 0 (or deleted IN (allowed values))","Use NOT IN with the enumerated remaining values if the domain is small and fixed","Remove or disable IllegalSQLInnerInterceptor if inequality filters are required by the domain"],"exampleFix":"// before (MyBatis-Plus wrapper)\nnew QueryWrapper<User>().ne(\"deleted\", 1);\n// after\nnew QueryWrapper<User>().eq(\"deleted\", 0);","handlingStrategy":"try-catch","validationCode":"// Detect != / <> predicates before execution\nExpression where = ((PlainSelect) ((Select) CCJSqlParserUtil.parse(sql).getSelectBody()).getSelectBody()).getWhere();\nif (where instanceof NotEqualsTo) {\n    throw new IllegalArgumentException(\"!= / <> in WHERE blocked by IllegalSQLInnerInterceptor\");\n}","typeGuard":"static boolean isNotEquals(Expression where) {\n    return where instanceof NotEqualsTo;\n}","tryCatchPattern":"try {\n    return mapper.findActive();\n} catch (MybatisPlusException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"【!=】\")) {\n        return mapper.findActiveByIn(); // equality allow-list variant\n    }\n    throw e;\n}","preventionTips":["Model exclusive states as explicit values (deleted=0/1) and filter with equality","Avoid QueryWrapper .ne() when the interceptor is active","When a value domain is open-ended, reconsider whether the interceptor rule fits that query"],"tags":["mybatis-plus","sql","not-equals","interceptor"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}