从JPA查询返回页面对象

时间:2018-12-13 20:43:09

标签: java spring jpa spring-data-jpa

我想通过分页创建JPA。我试图实现这一点:

@Override
    public Page<PaymentTransactions> findAll(Specification<PaymentTransactions> spec, Pageable pageable) {

        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();      

        String hql = "select e from " + PaymentTransactions.class.getName() + " e LIMIT :limit OFFSET :offset";
        TypedQuery<PaymentTransactions> query = entityManager.createQuery(hql, PaymentTransactions.class).setParameter("limit", pageSize).setParameter("offset", pageNumber);
        Page<PaymentTransactions> paymentTransactions = (Page<PaymentTransactions>) query.getResultList();
        return paymentTransactions;
    }

如何在不使用强制转换的情况下返回Page对象?

编辑我也尝试过这个:

终点:

@Autowired
private PaymentTransactionRepository transactionRepository;

@GetMapping
public Page<PaymentTransactionsDTO> page(@PathVariable int page, @PathVariable int size) {
        return transactionRepository
                .findAll(page, size)
                .map(mapper::toDTO);
    }

存储库:

public interface PaymentTransactionRepository extends CrudRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> {

    @Query(nativeQuery=true, 
            value="SELECT * FROM payment_transactions \n-- #pageable\n",
            countQuery="SELECT count(*) FROM payment_transactions")
    Page<PaymentTransactions> findAll(Pageable page);
}

编辑2。 我也尝试过:

@GetMapping("page")
    public Page<PaymentTransactionsDTO> page(@PathVariable int page, @PathVariable int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return transactionRepository.findAll(pageRequest).map(mapper::toDTO);
    }

接口:

public interface PaymentTransactionRepository extends CrudRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> {

    Page<PaymentTransactions> findAll(Pageable page);
}

实施:

@Override
    public Page<PaymentTransactions> findAll(Pageable page) {
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();

        String hql = "select e from " + PaymentTransactions.class.getName() + " e LIMIT :limit OFFSET :offset";
        TypedQuery<PaymentTransactions> query = entityManager.createQuery(hql, PaymentTransactions.class)
                .setParameter("limit", pageSize).setParameter("offset", pageNumber);
        Page<PaymentTransactions> paymentTransactions = (Page<PaymentTransactions>) query.getResultList();
        return paymentTransactions;
    }

3 个答案:

答案 0 :(得分:1)

要获得分页和排序功能,我们必须扩展PagingAndSortingRepositoryJpaRepository接口。

对于您而言,我们可以通过以下代码实现相同的目的:

终点:

@Autowired
private PaymentTransactionRepository transactionRepository;

@GetMapping
public Page<PaymentTransactionsDTO> page(@PathVariable int page, @PathVariable int size) {
    PageRequest pageRequest = PageRequest.of(page, size);
    return transactionRepository
            .findAll(pageRequest)
            .map(mapper::toDTO);
}

存储库:

public interface PaymentTransactionRepository extends JpaRepository<PaymentTransactions, Integer> {
    Page<PaymentTransactions> findAll(Pageable page);
}

请尝试一下,它应该可以工作。

答案 1 :(得分:1)

我已经完成了2个SQL查询。可能会有用。

  1. 为列表对象创建SQL并添加页面参数:

    query.setMaxResults(pageable.getPageSize());
    query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
    
  2. 使用包装的选择计数(*)创建相同的SQL

    countQuery.getSingleResult();
    

然后将其制作为paga对象

Page<ViewContract> page = (Page<ViewContract>) new PageImpl(
              selectQueryResultsList, 
              pageable, 
              countQueryResult);

答案 2 :(得分:0)

如果正确使用Sepcification API,则无需执行任何操作。 Spring data JPA将为您做到这一点。无论如何,在您实现的方法中,我都看不到“ spec”变量的使用,因此我认为它是错误的。

对于其余代码,只需遵循“春季之路”,定义一个存储库即可。使用@Query Annotation编写查询,如果要执行本机查询,请设置native = true。 Spring data JPA也支持本机查询中的分页,您只需要提供一个countQuery。

使用本地SQL查询和分页,您的界面应类似于:

public interface PaymentTransactionsRepository extends JpaRepository<PaymentTransactions, Integer>{


@Query(nativeQuery=true, 
        value="SELECT * FROM payment_transactions \n-- #pageable\n",
        countQuery="SELECT count(*) FROM payment_transactions")
Page<PaymentTransactions> findAll(Pageable page);

}

对于JPA查询,过程很简单,只需使用@Query注释编写查询即可。