如何获得Spring Data的总页数?

Gab*_*ado 3 java pagination repository spring-data-jpa

我有一个显示实体列表的Angular应用程序,还有一个“显示更多”按钮,可增加页码并使用此方法:

Page<myEntity> result = myRepo.findByAttr(attr, page);
Run Code Online (Sandbox Code Playgroud)

result将其格式化并通过REST的JSON发送。如果没有其他页面可供获取,我想禁用“显示更多”按钮。有一种“框架”特定的方式来检索此数字,或者我应该使用findAll()并计算该列表?

pvp*_*ran 15

这是Page接口 的源代码

public interface Page<T> extends Slice<T> {

    /**
     * Returns the number of total pages.
     * 
     * @return the number of total pages
     */
    int getTotalPages();

    /**
     * Returns the total amount of elements.
     * 
     * @return the total amount of elements
     */
    long getTotalElements();

    /**
     * Returns a new {@link Page} with the content of the current one mapped by the given {@link Converter}.
     * 
     * @param converter must not be {@literal null}.
     * @return a new {@link Page} with the content of the current one mapped by the given {@link Converter}.
     * @since 1.10
     */
    <S> Page<S> map(Converter<? super T, ? extends S> converter);
}
Run Code Online (Sandbox Code Playgroud)

您必须getTotalElements()获得匹配元素的总数。
getTotalPages()将给出总页数。


Mạn*_*yễn 5

使用result.getTotalElements()得到匹配的元素的总数。

使用result.getTotalPages()获得页面的总数。

ps result.getContent()用于获取内容为List <>