Pageable in spring 无法在 List<Object> 上分页

Pageable in spring for paging on List<Object> is not working

我有一个包含 10000 条记录的对象列表,我试图将这些记录分成 10 个,

但不知何故它不起作用..有人可以看看吗

@Query("select a.applicantid,coalesce(to_char(a.createdon,'yyyy-MM-dd'),to_char(filing_date,'yyyy-MM-dd')) as dt1, \r\n" + 
        "coalesce(c.companyname,i.InstituteName,(u.firstname||' '||u.lastname),\r\n" + 
        "u.firstname) ,a.c_denomination,cc.crop_common_name,cs.crop_botanical_name,\r\n" + 
        "a.id,aps.status,a.cropid, \r\n" + 
        "(select mv.varietytype from VarietyType mv where mv.id= a.varirtytypeid),\r\n" + 
        "(select sv.subvarietytype from SubVarietyType sv,VarietyType mvr \r\n" + 
        " where a.subvarietytypeid = sv.id and mvr.id= sv.varietyid),a.formtype,mcg.crop_group \r\n" + 
        " from Applications a left join ApplicantRegistration ap on \r\n" + 
        " a.applicantid = ap.id left join CompanyRegistration c on ap.companyid = c.id \r\n" + 
        " left join InstitutionRegistration i on ap.institutionid = i.id \r\n" + 
        " left join Crops cc on a.cropid = cc.id left join CropSpecies cs \r\n" + 
        " on a.cropspeciesid =cs.id left join InternalUser u on ap.id = u.applicantid \r\n" + 
        " left join ApplicationStatus aps on a.application_current_status = aps.id "
        + "left join CropGroup mcg on cc.cropgroupid = mcg.id order by a.id desc")
       List<Object[]> getapplication_adminview();



List<Object[]> admin_viewapplication=applicationrepository.getapplication_adminview();
int pageNumber = 0;
int size = 10;
Pageable pageable = PageRequest.of(pageNumber, size); // object of pageable
Page<Object> pages = new PageImpl(admin_viewapplication, pageable, admin_viewapplication.size());
List<Object> lpage = pages.getContent(); // here i am getting the lpage size as 10000 but as i enter pageable as of size 10  i am expecting 10 results only

我哪里错了? 如果我尝试将可分页对象添加到查询和 运行 代码中,我将收到以下错误:

Cannot create TypedQuery for query with more than one return using requested result type [java.lang.Long]; nested exception is java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [java.lang.Long]

Page就代表一页数据。所以 page.getContent() 只有 return 一个页面中的所有数据,这是在创建此页面实例时通过构造函数指定的。与分页数据无关

如果要拆分列表,使用 ListsGuava 是最简单的方法:

List<List<Object>> splittedList = Lists.partition(list, 10);

如果你想做分页将所有存储在数据库中的数据拆分成不同的小页面,在数据库级别拆分它而不是将整个列表放到内存中进行拆分,这将非常低效,当整个名单很大。请参阅 this 以了解如何通过在查询方法中声明 Pageable 在数据库级别拆分它。

我们可以使用 PagedListHolder,它可以更改页面中的列表,然后我们可以通过设置页面大小和页面来获取页面。

PagedListHolder<Object> page = new PagedListHolder(admin_viewapplicationpage);
      page.setPageSize(50); // number of items per page
      page.setPage(0);      // set to first page

      int totalPages = page.getPageCount(); //  gives the totalpages according to the main list
      
      List<Object> admin_viewapplication = page.getPageList();  // a List which represents the current page which is the sublist
      

以下教程对我有帮助 -> https://www.baeldung.com/spring-data-jpa-query

至此4.3。 Spring 2.0.4 之前的数据 JPA 版本

添加 \ n-- #pageable \ n

非常重要

没有这个我就错了

另外分页设置必须是无序

PageRequest paginaConf = new PageRequest ((param1 - 1)
                                                 , param2);

最后转换页面

  Page <Object []> list = myQueryofRepo ();
         List <XXXModel> lstReturn = myConversor (list.getContent ());
         Page <XXXModel> ret = new PageImpl <XXXModel> (lstReturn, pageConf, param2);