您好,登錄后才能下訂單哦!
這篇文章主要介紹了JPA與mybatisplus不兼容問題如何解決的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇JPA與mybatisplus不兼容問題如何解決文章都會有所收獲,下面我們一起來看看吧。
引入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原代碼 分析,發(fā)現(xiàn)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"); }
關(guān)于“JPA與mybatisplus不兼容問題如何解決”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“JPA與mybatisplus不兼容問題如何解決”知識都有一定的了解,大家如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。