当前位置: 首页 > 知识库问答 >
问题:

Spring启动-分页查询

景宏朗
2023-03-14

我想使用返回页面对象的注释@query在spring boot中编写一个查询。

所以,我的问题是:分页的最佳JPA查询类型(JPQL,NativeQuery,...)是什么?

提前谢谢你!

共有1个答案

苏运良
2023-03-14

对于分页,我使用org。springframework。数据存储库。分页和排序存储库这允许存储库返回页面

存储库代码:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ItemRepository 
extends JpaRepository<Item, Long> , PagingAndSortingRepository<Item, Long>{
    
}

服务代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class ItemService {

    @Autowired
    private ItemRepository repository;

    public Page<Item> findItems(Pageable pageable){
        return repository.findAll(pageable);        
    }
        
}

控制器代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path="api/v1/")
@CrossOrigin(origins = "http://localhost:4200")
public class ItemController {

    @Autowired
    private ItemService service;
    
    @GetMapping("/items")
    public Page<Item> findItems(@PageableDefault(size = 5) Pageable pageable) {     
        return service.findItems(pageable);
    }
    
}

反应的身体

{
    "content": [ ... ], // contain your objects 
    "pageable": {
        "sort": {
            "sorted": false,
            "unsorted": true,
            "empty": true
        },
        "offset": 0,
        "pageNumber": 0,
        "pageSize": 5,
        "unpaged": false,
        "paged": true
    },
    "last": false,
    "totalElements": 22,
    "totalPages": 5,
    "size": 5,
    "number": 0,
    "sort": {
        "sorted": false,
        "unsorted": true,
        "empty": true
    },
    "numberOfElements": 5,
    "first": true,
    "empty": false
}

我希望这能有所帮助,祝你愉快!

 类似资料:
  • 我正在开发一个Spring Boot应用程序,它查询DynamoDB以检索项目列表。我想使用分页并跳转到特定页面。 Eg. url:/items?页码=10 响应:{项目:[{item1},{item2},{item3}...{item10}],"页面": 3,"perPage": 10,"总计": 4000} DynamoDB是否支持这样的分页,其中一个可以跳转到一个特定的页面,而无需迭代其他页

  • 我有一个简单的查询如下“select * from USERS”。我还使用Pageable来启用分页。 此查询可能具有基于给定参数是否为 null 的可选谓词。 例如,如果给定了“code”参数且该参数不为空,则查询变为“select * from USERS where code =:code”; 据我所知,我不能使用@Query注释来实现这一点。我可以实现一个定制的存储库,并使用EntityM

  • “查询分析器”工具为查询日志提供图形表示,使你能够监控和优化查询性能,可视化查询活动统计数据,分析 SQL 语句,快速识别和解决长时间运行的查询。若要开始使用查询分析器,请在左侧窗格中选择要分析的实例,分析会立即开始。分析完成后,结果会显示出来: Navicat Monitor 每 60 秒刷新查询分析器中的指标。若要停止或开始刷新指标,请点击 或 图标。服务器数据收集在停止期间不会停止。 最新的

  • 在我的情况下,我有一个审查实体,它由审查状态和提交日期组成。 我正在尝试使用JpaRepository,但我不确定如何使用它来支持以下搜索查询。 我可以看到(参考http://docs.spring.io/spring-data/jpa/docs/1.4.3.release/reference/html/jpa.repositories.html),我们可以为特定的查询定义方法,比如FindByN

  • 我正在尝试测试以下控制器: 下面是测试类: 当我运行测试失败,并给我以下消息: 当我测试的url正常工作。

  • 我想做一些有趣的事情,我希望能够动态地构建SQL查询过滤器使用Spring Boot 1.5.9和Spring Data Rest,而不需要编写控制器。我觉得我可能走在正确的道路上,但我有点卡住了。 这个想法是通过使用HandlerInterceptorAdapter截取HTTP请求GET方法,并将请求查询参数存储到PagingAndSortingRepository可以使用的对象中。计划是重写S