如何在Spring Data(JPA)派生的查询中按多个属性排序? [英] How to sort by multiple properties in Spring Data (JPA) derived queries?

查看:391
本文介绍了如何在Spring Data(JPA)派生的查询中按多个属性排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此页面上提供的示例( http://docs.spring.io/spring-data/jpa/docs/current/reference/html/jpa.repositories.html )有关方法命名的信息,是否可以创建一个复杂的链方法名称,例如

I'm looking at the examples giving on this page (http://docs.spring.io/spring-data/jpa/docs/current/reference/html/jpa.repositories.html) about method naming, is it possible to create a complex chain method name such as

findByProgrammeAndDirectorAndProgDateBetweenOrderByProgDateStartTimeAsc

在他们给出的示例中,他们仅对一个值执行OrderBy.在上面的示例中,ProgDateStartTime将是两个单独的值.

In the example they give, they are only doing an OrderBy on one value. In the example above ProgDate and StartTime would be two separate values.

推荐答案

诀窍是使用方向关键字AscDesc来简单地界定要排序的属性.因此,您在查询方法中可能想要的是:

The trick is to simply delimit the properties you want to sort by using the direction keywords Asc and Desc. So what you probably want in your query method is something like:

…OrderByProgDateAscStartTimeAsc

请注意,我们如何通过Asc得出第一个属性定义,并继续处理下一个属性.

Note, how we conclude the first property definition by Asc and keep going with the next property.

通常来说,一旦方法名称超过一定长度或复杂度,我们建议切换到基于@Query的查询.主要原因是客户调用这些很长的方法很尴尬.使用@Query,您宁可获得查询语言的全部功能以及合理大小的方法名称,该名称可能是高级语言,以表达查询的意图.

Generally speaking, we recommend switching to @Query based queries, once method names exceed a certain length or complexity. The main reason being that it's awkward for clients to call these very long methods. With @Query you rather get the full power of the query language plus a reasonably sized method name that might be of higher level language to express the intent of the query.

这篇关于如何在Spring Data(JPA)派生的查询中按多个属性排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆