【问题标题】:How to run between and like clause while using Pagination and Sorting With Spring Data JPA如何在使用 Spring Data JPA 进行分页和排序时运行 between and like 子句
【发布时间】:2015-10-21 08:27:58
【问题描述】:

我必须执行一个查询

SELECT * FROM transaction WHERE account LIKE '%deepak%' and date_created BETWEEN 'Thu Jan 01 00:00:00 IST 2015' AND 'Wed Dec 16 00:00:00 IST 2015' ORDER BY date_created ASC OFFSET 5 LIMIT 10;

目前我能够使用 JPA 执行 4 个功能,它们是:-

  • 设置偏移量
  • 设置页面大小
  • 设置方向
  • 设置按列名排序

使用以下代码:-

PageRequest request = new PageRequest(1, 10, Sort.Direction.ASC, date_created );
return transactionRepository.findAll(request);

但是如何使用 Spring Data JPA 进行分页和排序来执行剩余的功能,即 'between' 子句和 'like' 子句

也欢迎其他建议的方法。

【问题讨论】:

    标签: spring hibernate jpa spring-boot spring-data-jpa


    【解决方案1】:

    看看Spring jpa data reference documentation,

    将此添加到扩展 PagingAndSortingRepository<..,..> 的 repository

    findByAccountLikeAndDateCreatedBetween(String account,Date start,Date end,Pageable pageable)
    

    【讨论】:

      【解决方案2】:

      如果您熟悉 JPQL,您始终可以在存储库中使用 @Query 注释查询。即:

      @Query("select t from transaction t where t.name like ?1")
      List<Transaction> getTransactions(String name, Pageable pageable)
      

      其中 ?1 是您传递的第一个参数,在本例中为名称。请注意,Pageable 必须始终是最后一个参数。这样您就可以随意命名您的查询。

      查看更多信息here。

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 1970-01-01
        • 1970-01-01
        • 2018-01-03
        • 1970-01-01
        • 2020-03-06
        • 1970-01-01
        • 2023-03-15
        • 2022-06-10
        相关资源
        最近更新 更多