获取Spring Data Rest中特定项目的页面数

huangapple go评论87阅读模式
英文:

Get page number of specific item in spring data rest

问题

我正在使用spring-data-rest构建一个网络应用程序。

我想在我的前端以分页表格的形式显示来自大型数据库表的数据。该表格会异步加载来自API的当前页面数据。通过只需拥有一个类似于以下的存储库,一切都可以正常运行:

public interface KeywordRepository extends JpaRepository<Keyword, String>, QuerydslPredicateExecutor<Keyword> {
}

现在,我想实现一种功能,使我能够在分页表格中跳转到包含特定项目的正确页面。问题是,我不知道特定项目位于哪个页面。

我需要一种类似于findPageOfItemById(Long id, Pageable pageable)的端点,以根据当前的筛选和排序参数告诉我特定项目(按ID)所在的页面号。

我该如何实现这个?

由于表格相当大,我不想将整个内容保存在内存中。

英文:

I'm building a web application with spring-data-rest.

I want to show data from a big db-table in a paginated table in my frontend. The table loads asynchronically just the current page from the API. That works all fine and dandy out of the box by just having a repository like

public interface KeywordRepository extends JpaRepository&lt;Keyword, String&gt;, QuerydslPredicateExecutor&lt;Keyword&gt; {
}

Now I want to implement a functionality to jump in my paginated table to the correct page where a certain item is.
Problem is that I don't know on what page that specific item is.

I need some kind of endpoint to tell me the page number of a specific item (by id) according to the current filter- and sorting-parameters. Basically findPageOfItemById(Long id, Pageable pageable).

How can I get this?

Since the table is quite big, I don't want to have the whole content in memory.

答案1

得分: 0

为了保证完整性,我会回答自己的问题。

这个方法运行得相当不错,而且不需要将整个列表加载到内存中,就像所需的那样。
但是我仍然希望能找到更多关于spring-data-rest的答案。

@GetMapping("/getPositionOfItem/{id}")
public long getPositionOfItem(@PathVariable String id, @QuerydslPredicate(root = SomeEntity.class) Predicate predicate, Pageable pageable) {
    Iterable<SomeEntity> elements = someEntityRepository.findAll(predicate, pageable.getSort());
    return findFirst(elements.iterator(), id);
}

private long findFirst(Iterator<SomeEntity> iterator, String id) {
    long index = 0;
    while (iterator.hasNext()) {
        if (iterator.next().getId().equals(id)) {
            return index;
        }
        index++;
    }
    return -1;
}

注意,我计算了项目的位置。要获取元素所在的页数,我们需要除以页面大小。

英文:

For the sake of completeness, I'll answer my own question.

It works quite nice and without loading the whole list into memory as reqested.
But I was still hoping to find a little more spring-data-resty answer.

    @GetMapping(&quot;/getPositionOfItem/{id}&quot;)
    public long getPositionOfItem(@PathVariable String id, @QuerydslPredicate(root = SomeEntity.class) Predicate predicate, Pageable pageable) {
        Iterable&lt;SomeEntity&gt; elements = someEntityRepository.findAll(predicate, pageable.getSort());
        return findFirst(elements.iterator(), id);
    }

    private long findFirst(Iterator&lt;SomeEntity&gt; iterator, String id) {
        long index = 0;
        while (iterator.hasNext()) {
            if (iterator.next().getId().equals(id)) {
                return index;
            }
            index++;
        }
        return -1;
    }

Note that I calculate the position of the item. To get the page of the element, we need to divide by the pagesize.

huangapple
  • 本文由 发表于 2020年3月16日 03:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60696668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定