java - 在 Spring 中将对象列表转换为页面

标签 java spring hibernate spring-data-jpa

我正在尝试获取每个页面中包含多个项目的页面。由于某种原因,此方法返回完整列表,而不是包含 5 个项目的页面。

public Page<Item> searchPagedCategoryByName(@RequestParam String name) 
{
    Category category;
    category = categoryRepository.findCategoryByCategoryName(name);
    List<Item> items = category.getItems();

    Pageable pageable = PageRequest.of(0,5);
    Page<Item> page = new PageImpl<Item>(items, pageable, items.size());
    return page;
}

最佳答案

创建扩展PagingAndSortingRepository的存储库,它提供执行分页和排序功能,然后您可以像这样使用,

这是我的练习工作区中的代码片段。

public interface CategoryRepository extends PagingAndSortingRepository< Category, Long> {
  List<Category> findCategoryByCategoryName(String categoryName, Pageable pageable);
}

@Bean
public CommandLineRunner pagingAndSortingRepositoryDemo(CategoryRepository repository) {
    return (args) -> {
        log.info("Category found with Paging Request PageRequest.of(page [zeroBased Page index], Size)");
        repository. findCategoryByCategoryName(name , PageRequest.of(0, 5)).forEach(category -> log.info(" :=> " + category));
    };
}

关于java - 在 Spring 中将对象列表转换为页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52552072/

相关文章:

java - Hibernate 更改 @Formula 中的别名

java - Hibernate:如何保留顺序相关信息

java - 类 GenericMemoryCell<T> 中的 GenericMemoryCell 无法应用于给定类型

java - 自动递增 ID 打破序列

java - Spring Boot 不为 ZonedDateTime 使用自定义反序列化器

linux - 特殊字符与linux不同的窗口

java - 无法运行 Spring Hello World 不断收到 NoClassDefFoundError

java - 无法将 mssql 时间戳列映射到 hibernate

c# - Java 到 C# - CallableStatement 转换

java - 尽管使用了正确的公钥和签名文件,但签名未得到验证