【问题标题】:Using findAll PagingAndSortingRepository with filter使用带有过滤器的 findAll PagingAndSortingRepository
【发布时间】:2017-05-31 08:41:11
【问题描述】:

我需要使用 PagingAndSortingRepository 来获取元素列表。我还需要使用 RequestParam 过滤此列表。

在我的控制器中:

@RequestMapping(path = "/listfilter", method = RequestMethod.GET)
public Page<Element> getElements(
        @RequestParam("page") int page,
        @RequestParam("size") int size,
        @RequestParam("aaa") String filter) {

    Filter filter = new Filter();
    filter.setPageNum(page);
    filter.setPageSize(size);
    filter.setAaa(aaa);

    return controller.findElements(filter);
}

如何将过滤后的列表传递给方法

Page<Element> findElements(Pageable pageable) {...}

谢谢

【问题讨论】:

  • 将其与JpaSpecificationExecutor 组合... 这允许您定义Specification 并将其与分页组合。

标签: java spring spring-mvc spring-boot


【解决方案1】:

假设 'aaa' 是一个字符串(用户名或 smth),使用 PagingAndSortingRepository 中的 Spring JPA,您可以执行以下操作:

Page&lt;Element&gt; findAllByAaa(String aaaValue, Pageable pageable);

网址可能类似于:/elements/aaa?page=pageNr&amp;size=nrOfElemOnPage

它会在你的table上迭代,选择aaa = aaaValue所在的元素,放入Pageable中

在我看来,这是一种更好、更清洁的方法。

【讨论】:

  • 是的,事实上我正朝着这个方向前进,但我对 Pageable 类并不那么热衷:它如何修复我从请求中收到的页码和页面大小?谢谢
  • 可以使用方法(@PathVariable("aaa")String aaa, @RequestParam(value = "size")String size, @RequestParam(value = "page")String page)
  • 好极了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-01-19
  • 2020-04-27
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-09-06
  • 2021-07-11
  • 2016-11-20
相关资源
最近更新 更多