将findAll PagingAndSortingRepository与过滤器一起使用

时间:2017-01-16 13:32:55

标签: java spring spring-mvc spring-boot

我需要使用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) {...}

谢谢

1 个答案:

答案 0 :(得分:4)

假设'aaa'是一个字符串(用户名或smth),在PagingAndSortingRepository中使用Spring JPA你可以这样做:

Page<Element> findAllByAaa(String aaaValue, Pageable pageable);

网址可能如下:/elements/aaa?page=pageNr&size=nrOfElemOnPage

它将在您的桌子上迭代,选择aaa = aaaValue的元素,将它们放入Pageable

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

相关问题