java 如何使用 QueryDSL 在 Spring Data JPA 中使用 order by 和 Limit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6356322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:32:19  来源:igfitidea点击:

How to use order by and Limit in Spring Data JPA using QueryDSL

javadatabasequerydslspring-data

提问by Abhishek

I am using Spring Data JPA with a RDBMS in my project. I have a requirement where I have to fetch a single record from the Database from a table which has the latest date. For this I need to use a limit and order by function OR using sub queries. However, I wished to know if i wish for not to use NamedQuery is there a way I can achieve this using Spring Data JPA and QueryDSL.

我在我的项目中使用带有 RDBMS 的 Spring Data JPA。我有一个要求,我必须从具有最新日期的表中从数据库中获取一条记录。为此,我需要使用限制和按函数排序或使用子查询。但是,我想知道是否不希望使用 NamedQuery,是否可以使用 Spring Data JPA 和 QueryDSL 来实现这一点。

回答by Oliver Drotbohm

Shouldn't QueryDslPredicateExecutor.findAll(Predicate predicate, Pageable pageable)do the trick for you? You could hand in a new PageRequest(0, limit)and thus would get back the first limitresults.

不应该QueryDslPredicateExecutor.findAll(Predicate predicate, Pageable pageable)为你做伎俩吗?您可以提交 a new PageRequest(0, limit),从而返回第一个limit结果。

回答by Talha Ahmed Khan

How about using the MIN and MAX function to attain this.

如何使用 MIN 和 MAX 函数来实现这一点。

SELECT MAX(objDate) FROM Object