Spring boot Spring数据JDBC-可在自定义查询中分页

Spring boot Spring数据JDBC-可在自定义查询中分页,spring-boot,spring-data-jdbc,pageable,Spring Boot,Spring Data Jdbc,Pageable,在我的项目中,我有一个扩展Crudepository的存储库。在这里,我有一个自定义查询: public interface CustomerRepository extends CrudRepository<Customer, Long> { @Query("select * from person where firstname = :firstname") List<Customer> findByFirstn

在我的项目中,我有一个扩展Crudepository的存储库。在这里,我有一个自定义查询:

    public interface CustomerRepository extends CrudRepository<Customer, Long> {
       @Query("select * from person where firstname = :firstname")
       List<Customer> findByFirstname(@Param("firstname") String firstname, Pageable pageable);
    }
但这感觉不对。还有一个原因是加载列表中的所有数据,而不仅仅是通过pageable参数化的大小和偏移量获取数据

备注:如果使用存储库中的页面,我将获得org.springframework.dao.incorrectressultsizedataaccessException:结果大小不正确:预期为1,实际为88

Jira还有一个公开的改进,可以在这里找到:


希望得到一些帮助……

我从德克·路易克(Thx-Dirk:)那里得到了关于JIRA问题的回复

接口FooRepository扩展了分页和排序存储库{
列表findAllByBar(字符串栏,可分页);
长countAllByBar(字符串条);
}
然后像这样组合这两个查询:

List<FooEntity> fooList = repository.findAllByBar("...", pageable);
Long fooTotalCount = repository.countAllByBar("...");

Page<FooEntity> fooPage = PageableExecutionUtils.getPage(fooList, pageable, () -> fooTotalCount);
List-dublist=repository.findAllByBar(“…”,可分页);
Long FootTotalCount=repository.countAllByBar(“…”);
Page fooPage=pageableexecutionils.getPage(傻瓜列表,可分页,()->foottalcount);
“解决方法中的错误是自定义查询。在Spring Data JDBC 2.0中,除特殊查询外,不需要使用自定义查询,但它们不支持分页。”

可以找到可能的参数:

Thx德克

我还找到了一种解决方法,可以让它通过自定义查询运行。只需使用limit、offset和orderBy作为附加参数,如下所示:

    @Query("select * from person where firstname = :name order by :order limit :size offset :offset")
    List<Customer> findByFirstNameCustomQuery(@Param("name") String name, Pageable page, @Param("offset") long offset,
            @Param("size") long size, @Param("order") String order);
@Query(“从firstname=:name order by:order limit:size offset:offset的人员中选择*)
列出findByFirstNameCustomQuery(@Param(“name”)字符串名称,可分页页面,@Param(“offset”)长偏移量,
@参数(“大小”)长尺寸,@Param(“顺序”)字符串顺序);
然后在服务内部更改呼叫,如:

List<Customer> custList = customerRepository.findByFirstNameCustomQuery(firstname, pageable, ....
List custList=customerRepository.findByFirstNameCustomQuery(firstname,可分页,…)。。。。
List<FooEntity> fooList = repository.findAllByBar("...", pageable);
Long fooTotalCount = repository.countAllByBar("...");

Page<FooEntity> fooPage = PageableExecutionUtils.getPage(fooList, pageable, () -> fooTotalCount);
    @Query("select * from person where firstname = :name order by :order limit :size offset :offset")
    List<Customer> findByFirstNameCustomQuery(@Param("name") String name, Pageable page, @Param("offset") long offset,
            @Param("size") long size, @Param("order") String order);
List<Customer> custList = customerRepository.findByFirstNameCustomQuery(firstname, pageable, ....