天天看點

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended錯誤

看下完整的異常

2018-10-19 20:13:34: INFO [http-nio-8080-exec-10] o.s.b.f.x.XmlBeanDefinitionReader XmlBeanDefinitionReader.java:317 - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2018-10-19 20:13:34: INFO [http-nio-8080-exec-10] o.s.j.s.SQLErrorCodesFactory SQLErrorCodesFactory.java:127 - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
2018-10-19 20:13:34: WARN [http-nio-8080-exec-10] c.e.c.w.a.ControllerExceptionAdvice ControllerExceptionAdvice.java:44 - RuntimeException
2018-10-19 20:13:34: WARN [http-nio-8080-exec-10] c.e.c.w.a.ControllerExceptionAdvice ControllerExceptionAdvice.java:45 -
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

### The error may exist in class path resource [mapper/StartMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT *         FROM start         ORDER BY "last_updated" DESC LIMIT ?
### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
           

說下場景,我用的是Mybatis對接MySQL,并且使用了分頁插件,一切都是OK的。現在有需求要同時相容Oracle,切換的時候出現上面異常。

現在直接給出問題原因(我隻能說是我問題的答案,至于其它場景引起的這個異常不一定合适)

database:
  type: oracle

mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    database-id: ${database.type}
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

pagehelper:
  auto-dialect: true
  close-conn: true
  offset-as-page-num: true
  helper-dialect: ${database.type}
  reasonable: true
           

問題就出在pagehelper的helper-dialect上,這裡必須要指定成Oracle。

其實我jar包外(Linux伺服器上)的配置application.yml中的配置是oracle,這點沒問題的,application-prod.yml中沒有這個配置,但是打包的時候項目中的application-prod.yml是之前的代碼有這個配置,設定的是mysql。

總之我的這個異常是pagehelper的資料庫類型不比對引起的。