引入mybatis-plus后,在spring-boot启动时,JPA会使用CCJSqlParser对SQL进行分析处理,由于@Query中的Native-SQL语法并非完全的sql,所以,会报错
Caused by: java.lang.IllegalArgumentException: The query you provided is not a valid SQL Query!
at org.springframework.data.jpa.repository.query.JSqlParserQueryEnhancer.detectParsedType(JSqlParserQueryEnhancer.java:95)
at org.springframework.data.jpa.repository.query.JSqlParserQueryEnhancer.<init>(JSqlParserQueryEnhancer.java:72)
at org.springframework.data.jpa.repository.query.QueryEnhancerFactory.forQuery(QueryEnhancerFactory.java:45)
at org.springframework.data.jpa.repository.query.StringQuery.<init>(StringQuery.java:83)
at org.springframework.data.jpa.repository.query.DeclaredQuery.of(DeclaredQuery.java:40)
at org.springframework.data.jpa.repository.query.JpaQueryMethod.assertParameterNamesInAnnotatedQuery(JpaQueryMethod.java:158)
at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:139)
at org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory.build(DefaultJpaQueryMethodFactory.java:44)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103)
... 66 common frames omitted
Caused by: net.sf.jsqlparser.JSQLParserException: Encountered unexpected token: "<=" <OP_MINORTHANEQUALS>
at line 1, column 1648.
Was expecting one of:
"&"
")"
"."
"::"Virgil??
"<<"
">>"
"COLLATE"
"["
"^"
"|"
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatement(CCJSqlParserUtil.java:190)
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:63)
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:38)
at org.springframework.data.jpa.repository.query.JSqlParserQueryEnhancer.detectParsedType(JSqlParserQueryEnhancer.java:83)
... 75 common frames omitted
Caused by: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "<=" <OP_MINORTHANEQUALS>
at line 1, column 1648.
Was expecting one of:
"&"
")"
"."
"::"
"<<"
">>"
"COLLATE"
"["
"^"
"|"
at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:31234)
at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:31067)
at net.sf.jsqlparser.parser.CCJSqlParser.AndExpression(CCJSqlParser.java:9663)
at net.sf.jsqlparser.parser.CCJSqlParser.OrExpression(CCJSqlParser.java:9609)
at net.sf.jsqlparser.parser.CCJSqlParser.XorExpression(CCJSqlParser.java:9591)
at net.sf.jsqlparser.parser.CCJSqlParser.Expression(CCJSqlParser.java:9562)
at net.sf.jsqlparser.parser.CCJSqlParser.WhenThenSearchCondition(CCJSqlParser.java:13048)
at net.sf.jsqlparser.parser.CCJSqlParser.CaseWhenExpression(CCJSqlParser.java:12940)
at net.sf.jsqlparser.parser.CCJSqlParser.PrimaryExpression(CCJSqlParser.java:11112)
at net.sf.jsqlparser.parser.CCJSqlParser.BitwiseXor(CCJSqlParser.java:10981)
at net.sf.jsqlparser.parser.CCJSqlParser.MultiplicativeExpression(CCJSqlParser.java:10934)
at net.sf.jsqlparser.parser.CCJSqlParser.AdditiveExpression(CCJSqlParser.java:10897)
at net.sf.jsqlparser.parser.CCJSqlParser.BitwiseAndOr(CCJSqlParser.java:10843)
通过 JPA原代码 分析,发现QueryEnhancerFactory中有一个代码 ,会去找JSqlParser
private static boolean isJSqlParserInClassPath() {
try {
Class.forName("net.sf.jsqlparser.parser.JSqlParser", false, QueryEnhancerFactory.class.getClassLoader());
LOG.info("JSqlParser is in classpath. If applicable JSqlParser will be used.");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
所以,为了不让JPA使用JSqlParser,在SpringBoot启动时,增加特殊处理代码
public static void main(String[] args) {
try {
QueryEnhancerFactory.forQuery(null) ;
} catch(Exception e) {
// just init class QueryEnhancerFactory, ignore anything
}
try {
Field JSQLPARSER_IN_CLASSPATH = QueryEnhancerFactory.class.getDeclaredField("JSQLPARSER_IN_CLASSPATH") ;
Field modifierField = Field.class.getDeclaredField("modifiers") ;
modifierField.setAccessible(true);
modifierField.setInt(JSQLPARSER_IN_CLASSPATH, JSQLPARSER_IN_CLASSPATH.getModifiers() & ~Modifier.FINAL);
JSQLPARSER_IN_CLASSPATH.setAccessible(true);
JSQLPARSER_IN_CLASSPATH.setBoolean(null, true);
} catch(Exception e) {
log.error(e.getMessage(), e) ;
}
SpringApplication.run(PmsApplication.class, args);
log.info("Start Success");
}
到此这篇关于JPA与mybatis-plus不兼容问题的解决的文章就介绍到这了,更多相关JPA与mybatis-plus不兼容内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!