PagingAndSortingRepository

PagingAndSortingRepository

本文介绍了将findAll PagingAndSortingRepository与过滤器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PagingAndSortingRepository以获得元素列表.我还需要使用RequestParam过滤此列表.

I have the need to use the PagingAndSortingRepository in order to obtain a list of element. I also need to filter this list with a 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);
}

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

How can I pass the filtered list to the method

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

谢谢

推荐答案

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

Supposing 'aaa' is a string (username or smth), with Spring JPA in PagingAndSortingRepository you could do something like this:

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

网址可能看起来像:/elements/aaa?page = pageNr& size = nrOfElemOnPage

它将在您的表上进行迭代,选择 aaa = aaaValue 的元素,并将其放入Pageable

It will iterate on your table, select the elements where aaa = aaaValue, put them into Pageable

我认为这是一种更好,更清洁的方法.

In my opinion this is a better and cleaner aproach.

这篇关于将findAll PagingAndSortingRepository与过滤器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 04:10