【发布时间】:2019-05-16 15:50:23
【问题描述】:
我使用此代码通过 JPA 和 Spring 获取 DB 行:
return transactionService.findAll(page, size)
public Page<PaymentTransactions> findAll(int page, int size) {
return dao.findAll(PageRequest.of(page,size, new Sort(Sort.Direction.DESC, "createdAt")));
}
我想为此代码实现相同的排序:
return transactionService.getAllBySpecification(specification, pageable)
@Override
public Page<PaymentTransactions> getAllBySpecification(final Specification<PaymentTransactions> specification, final Pageable pageable) {
return dao.findAll(specification, pageable);
}
你知道我如何使用规范按列实现排序方向吗?像这样的:
return dao.findAll(specification, PageRequest.of(page,size, new Sort(Sort.Direction.DESC, "createdAt")));
其他问题:
我可以设置带有排序方向的可分页对象吗?像这样的:
@Override
public Page<PaymentTransactions> getAllBySpecification(final Specification<PaymentTransactions> specification, final Pageable pageable) {
return dao.findAll(specification, PageRequest.of(pageable, new Sort(Sort.Direction.DESC, "createdAt")));
}
【问题讨论】:
-
究竟是什么问题?
dao的类是从 CrudRepository 派生的还是你的自定义类? -
Dao 是一个存储库:
public interface PaymentTransactionRepository extends JpaRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions>我找不到如何实现具有规范、分页和排序方向的 findAll。 -
我错过了什么吗?只要
JpaSpecificationExecutor已经提供了Page<T> findAll(Specification<T> spec, Pageable pageable);方法,就不需要实现它。排序设置包含在您的 PageRequest 中 -
hm....你能给我看一些代码示例吗?
-
就我而言,我想设置
int page, int size
标签: java spring spring-boot design-patterns spring-data