如何使用 Spring JPA 进行分页 QueryDSL 查询? [英] How to do a paged QueryDSL query with Spring JPA?

查看:76
本文介绍了如何使用 Spring JPA 进行分页 QueryDSL 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QueryDSL 定义了一个 OrderSpecifier 接口和一个实例,可以通过调用 asc()desc() 轻松获取任何字段的实例.Spring Data JPA 的 QueryDslPredicateExecutor 接口甚至有一个 findAll() 方法,它以 OrderSpecifiers 作为参数.

QueryDSL defines an OrderSpecifier interface and an instance for that can be easily obtained for any field by calling asc() or desc(). The QueryDslPredicateExecutor interface of Spring Data JPA even has a findAll() method which takes OrderSpecifiers as parameters.

org.springframework.data.domain.PageRequest 但是对 QueryDSL 一无所知,它有自己的定义查询排序顺序的方法,即 org.springframework.data.domain.Sort.它可以包含许多 org.springframework.data.domain.Sort.Orders,它们很像 OrderSpecifiers,除了它们不是类型安全的等等.

org.springframework.data.domain.PageRequest however doesn't know anything about QueryDSL and it has its own way for defining query sort order, namely org.springframework.data.domain.Sort. It can contain a number of org.springframework.data.domain.Sort.Orders which are a lot like OrderSpecifiers, except that they are not type safe etc.

那么,如果我想使用排序进行分页查询,真的没有办法使用 QueryDSL 来定义吗?

So, if I want to make paged query which uses sorting, is there really no way of using QueryDSL for defining that?

推荐答案

如果你找不到其他方法,它应该是这样工作的

It should work like this if you can't find another way

private Sort sortBy(Path<?> path) {
    return new Sort(Sort.Direction.ASC, path.getMetadata().getExpression().toString());
}

这篇关于如何使用 Spring JPA 进行分页 QueryDSL 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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