JPA与mybatisplus不兼容问题如何解决

其他教程   发布日期:2023年07月06日   浏览次数:459

这篇文章主要介绍了JPA与mybatisplus不兼容问题如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇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原代码 分析,发现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与mybatisplus不兼容问题如何解决的详细内容,更多关于JPA与mybatisplus不兼容问题如何解决的资料请关注九品源码其它相关文章!