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

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

问题描述

我正在查看此页面上给出的示例(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.repositories) 关于方法命名,是否可以创建一个复杂的链方法名如

I'm looking at the examples giving on this page (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.repositories) 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天全站免登陆