List分页的几种方法

List分页的几种方法,第1张

1.根据入参带分页参数进行sql查询分页

         Criteria criteria = new Criteria();
         //将dataAuto转成 factoryId brandId seriesId 等查询条件
         String dataAuth = "";
         TypeCaseHelper.dataAuto(criteria, dataAuth);
         // 设置分页信息
         ExtPager pager = new ExtPager();
         Integer startTure = start * pageSize;
         criteria.setMysqlPageSize(pageSize);
         criteria.setMysqlStart(startTure);
         // 排序信息
         if (StringUtils.isNotBlank(pager.getDir()) && StringUtils.isNotBlank(pager.getSort())) {
             criteria.setOrderByClause(pager.getSort() + " " + pager.getDir());
         }
         List listDistinct = getVhlAlarmStatusMgtEntities(vin, faultStatus, confirmStatus, startDateTime, endDateTime, sdf, sdfm, carTypeList, criteria);

对应的分页sql

  

2.对所有list根据分页参数分页

         Criteria criteria1 = new Criteria();
         List listDistinctForCout = getVhlAlarmStatusMgtEntities(vin, faultStatus, confirmStatus, startDateTime, endDateTime, sdf, sdfm, carTypeList, criteria1);
         Integer count = listDistinctForCout.size(); // 记录总数
         Integer pageCount; // 页数
         if (count % pageSize == 0) {
             pageCount = count / pageSize;
         } else {
             pageCount = count / pageSize + 1;
         }
         int fromIndex; // 开始索引
         int toIndex; // 结束索引
         if (!pageCount.equals(start+1)) {
             fromIndex = start * pageSize;
             toIndex = fromIndex + pageSize;
             if(toIndex > count){
                 fromIndex = (start-1) * pageSize;
                 toIndex = count;
             }
         } else {
             fromIndex = start * pageSize;
             toIndex = count;
         }
         List pageList = listDistinctForCout.subList(fromIndex, toIndex);

3.PageHelper分页

      //开启分页
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
 
        PageInfo> pageInfo = new PageInfo(datalist);

        HashMap map= new HashMap<>();
        map.put("datalist", datalist);
        map.put("total", pageInfo.getTotal());
        map.put("size", pageInfo.getPageSize());
        map.put("page", pageInfo.getPageNum());

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/991384.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存