{"record":{"id":"b3652c1591b5884a","repo":"baomidou/mybatis-plus","slug":"sql-where","errorCode":null,"errorMessage":"非法SQL，where条件中不能使用【!=】关键字，错误!=信息：{}","messagePattern":"非法SQL，where条件中不能使用【!=】关键字，错误!=信息：(.+?)","errorType":"exception","errorClass":"MybatisPlusException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java","lineNumber":179,"sourceCode":"    }\n\n    /**\n     * 验证expression对象是不是 or、not等等\n     *\n     * @param expression ignore\n     */\n    private void validExpression(Expression expression) {\n        while (expression instanceof Parenthesis) {\n            Parenthesis parenthesis = (Parenthesis) expression;\n            expression = parenthesis.getExpression();\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":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/IllegalSQLInnerInterceptor.java#L161-L197","documentation":"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.","triggerScenarios":"Executing SQL or a QueryWrapper-built statement with a 'column != value' predicate (e.g. .ne(...) producing status != 0) while IllegalSQLInnerInterceptor is registered.","commonSituations":"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.","solutions":["Rewrite != as a positive range/IN predicate where possible (e.g. status != 0 becomes status IN (1,2)).","Annotate the mapper method with @InterceptorIgnore(illegalSql = \"true\") to opt the specific statement out.","If the policy is too strict for the project, remove IllegalSQLInnerInterceptor from the MybatisPlusInterceptor chain."],"exampleFix":"// before\nwrapper.ne(User::getStatus, 0);\n\n// after\nwrapper.in(User::getStatus, 1, 2);","handlingStrategy":"validation","validationCode":"// Grep-level guard before release: forbid .ne( and '<>' / '!=' in mapped SQL\n// CI example: git grep -nE \"\\.ne\\\\(|!=|<>\" -- src/main/resources/mapper && exit 1","typeGuard":null,"tryCatchPattern":"try {\n    mapper.selectList(wrapper);\n} catch (MybatisPlusException e) {\n    if (String.valueOf(e.getMessage()).contains(\"不能使用【!=】\")) {\n        log.error(\"policy violation: replace != with IN/eq: {}\", e.getMessage());\n    }\n}","preventionTips":["Standardize positive predicates (eq / in) in wrapper style guides.","Add a CI grep or ArchUnit rule banning .ne() and <> in mapped SQL.","Document the interceptor policy for new team members so != does not reach production."],"tags":["mybatis-plus","sql-parsing","interceptor","policy","jsqlparser"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}