先看错误:
Error querying database. Cause: ***.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, Error SQL:*******省略若干
Caused by: ***.sf.jsqlparser.parser.ParseException: Encountered unexpected token: “(” “(” ********省略若干
直接说结论:
mybatisplus多租户使用sql拦截导致的不能识别sql语句问题
解决方法:
根据版本不同,用一下三种:
①在Mapper上加入注解:@InterceptorIgnore(tenantLine =“true”)或者@InterceptorIgnore(tenantLine =“0”),不走多租户过滤。
②老一点的版本用@SqlParser(filter=true)
③3.1.1以下版本添加如下配置
mybatis-plus:
global-config:
sql-parser-cache: true
总结
细究原因就是jsqlparser的问题,MybatisPlusInterceptor中使用了TenantLineHandler,如果既需要这个sql又需要拦截,可以在源码增加语法规则,一般没必要,实在不行在这儿手动增加tenant-id也可以。
部分主要错误如下:
java">Error querying database. Cause: ***.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, Error SQL: select * from (select avg(CAST((tag_value) AS DOUBLE)) as tagValue from qwe.qwe interval(1m))
Cause: ***.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, Error SQL: select * from (select avg(CAST((tag_value) AS DOUBLE)) as tagValue from qwe.qwe interval(1m))
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:153)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at sun.reflect.NativeMethodA***essorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodA***essorImpl.invoke(NativeMethodA***essorImpl.java:62)
at sun.reflect.DelegatingMethodA***essorImpl.invoke(DelegatingMethodA***essorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427)
... 133 ***mon frames omitted
Caused by: ***.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, Error SQL: select * from (select avg(CAST((tag_value) AS DOUBLE)) as tagValue from qwe.qwe interval(1m))
at ***.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:39)
at ***.baomidou.mybatisplus.extension.parser.JsqlParserSupport.parserSingle(JsqlParserSupport.java:52)
at ***.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor.beforeQuery(TenantLineInnerInterceptor.java:65)
at ***.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor.intercept(MybatisPlusInterceptor.java:78)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62)
at ***.sun.proxy.$Proxy447.query(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151)
... 140 ***mon frames omitted
Caused by: ***.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "(" "("
at line 1, column 109.
Was expecting one of:
")"
"CONNECT"
"EMIT"
"EXCEPT"
"GROUP"
"HAVING"
"INTERSECT"
"MINUS"
"PIVOT"
"START"
"UNION"
"WINDOW"
at ***.sf.jsqlparser.parser.***JSqlParser.generateParseException(***JSqlParser.java:31234)
at ***.sf.jsqlparser.parser.***JSqlParser.jj_consume_token(***JSqlParser.java:31067)
at ***.sf.jsqlparser.parser.***JSqlParser.FromItem(***JSqlParser.java:7931)
at ***.sf.jsqlparser.parser.***JSqlParser.PlainSelect(***JSqlParser.java:5618)
at ***.sf.jsqlparser.parser.***JSqlParser.SetOperationList(***JSqlParser.java:5827)
at ***.sf.jsqlparser.parser.***JSqlParser.SelectBody(***JSqlParser.java:5499)
at ***.sf.jsqlparser.parser.***JSqlParser.Select(***JSqlParser.java:5494)
at ***.sf.jsqlparser.parser.***JSqlParser.SingleStatement(***JSqlParser.java:232)
at ***.sf.jsqlparser.parser.***JSqlParser.Statement(***JSqlParser.java:153)
at ***.sf.jsqlparser.parser.***JSqlParserUtil.parseStatement(***JSqlParserUtil.java:188)
at ***.sf.jsqlparser.parser.***JSqlParserUtil.parse(***JSqlParserUtil.java:63)
at ***.sf.jsqlparser.parser.***JSqlParserUtil.parse(***JSqlParserUtil.java:38)
at ***.baomidou.mybatisplus.extension.parser.JsqlParserSupport.parserSingle(JsqlParserSupport.java:49)
... 145 ***mon frames omitted