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里执行 发现可以查出数据

那么这是什么原因呢?
经过分析Hibernate打印的SQL
select count(t.*) from .....
发现使用page分页会有一个查询所有数据的条数的操作,用于计数TotalElements

在dataGrip里执行SQL

复刻出了问题
解决方法
把原SQL select t.* from .... 里select t.* 改成select *就可以了。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot+Mybatis+Vue 实现商品模块的crud操作
这篇文章主要介绍了SpringBoot+Mybatis+Vue 实现商品模块的crud操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-10-10
Maven包冲突导致NoSuchMethodError错误的解决办法
web 项目 能正常编译,运行时也正常启动,但执行到需要调用 org.codehaus.jackson 包中的某个方法时,产生运行异常,这篇文章主要介绍了Maven包冲突导致NoSuchMethodError错误的解决办法,需要的朋友可以参考下2024-05-05


最新评论