Jpa使用Page和Pageable分頁遇到的問題及解決
報錯代碼
@Query(value = "select t.* from goods t where t.type =:type ", nativeQuery = true) Page<Goods> findAllByType(String type,Pageable pageable);
報錯信息
org.hibernate.exception.SQLGrammarException: could not extract ResultSet at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptio Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) from Table t where t.type ='A' com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.25.jar:8.0.25] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
分析
看報錯信息得知 是SQL出了問題 并且定位在 near '*)
但是把SQL放到DataGrip里執(zhí)行 發(fā)現(xiàn)可以查出數(shù)據(jù)
那么這是什么原因呢?
經(jīng)過分析Hibernate打印的SQL
select count(t.*) from .....
發(fā)現(xiàn)使用page分頁會有一個查詢所有數(shù)據(jù)的條數(shù)的操作,用于計數(shù)TotalElements
在dataGrip里執(zhí)行SQL
復(fù)刻出了問題
解決方法
把原SQL select t.* from .... 里select t.* 改成select *就可以了。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+Mybatis+Vue 實現(xiàn)商品模塊的crud操作
這篇文章主要介紹了SpringBoot+Mybatis+Vue 實現(xiàn)商品模塊的crud操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法
web 項目 能正常編譯,運行時也正常啟動,但執(zhí)行到需要調(diào)用 org.codehaus.jackson 包中的某個方法時,產(chǎn)生運行異常,這篇文章主要介紹了Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法,需要的朋友可以參考下2024-05-05@FeignClient?實現(xiàn)簡便http請求封裝方式
這篇文章主要介紹了@FeignClient?實現(xiàn)簡便http請求封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03