一、如何集成插件java
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.2</version> </dependency>
二、application-dev.properties配置信息(pageHelper使用默認配置,不寫也OK 的)mysql
###pagehelper### pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=pageNum=punt=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero pagehelper.row-bounds-with-count=false
三、看代碼,這個代碼是錯誤的,total,pages跟實際數據庫數據不一致。pages=1,total=1,而且該值永遠等於查詢頁的記錄數, 即getTotal和getSize()效果相同. 讀者若自行查看SQL日誌, 可發現startPage()方法成功執行, 返回的List是符合查詢條件的.啥緣由呢?git
網上有說1.2.0版本有問題,也有人說查出來的list,不能進行操做,不然會引發total根實際數據不一致的狀況,以下我對查出來的list把領域E轉成DTO了等等說法。github
public ResponseMessage<PagePing<DocTemplateDTO>> getDocTemplateList(DocTemplateDTO docTemplateDTO,Integer currentPage,Integer pageSize) { ResponseMessage<PagePing<DocTemplateDTO>> resp = new ResponseMessage<>(); PagePing<DocTemplateDTO> p=new PagePing<>(); try { PageHelper.startPage(currentPage,pageSize); DocTemplateE docTemplateE = this.docTemplateDomainFactory.convert(docTemplateDTO); List<DocTemplateE> list = docTemplateE.findList(docTemplateE); if (CollectionUtils.isNotEmpty(list)) { PageInfo<DocTemplateDTO> pageInfo = new PageInfo<>(docTemplateConverter.dataToDTO(list)); // 轉換成本身的分頁信息 PageBean<DocTemplateDTO> pb=new PageBean<>(); pb.setCurrentPage(pageInfo .getPageNum()); pb.setPageSize(pageInfo .getPageSize()); pb.setPageCount(pageInfo .getPages()); pb.setTotalData(pageInfo .getTotal()); p.setDataList(pageInfo .getList()); p.setPage(pb); ResponseUtil.setResult(resp, p, ErrorCodeEnum.SUCCESS, true); } else { ResponseUtil.setResult(resp,null, ErrorCodeEnum.NORECORD,false); } } catch (Exception e) { logger.error("查詢單證模板失敗", e); ResponseUtil.setResult(resp,null, ErrorCodeEnum.ERROR,false); } return resp; }
四、解決方法,對好比上。在設置Page page = (Page) PageHelper.startPage(currentPage,pageSize); 獲取page數據。由於PageInfo裏面 的page信息,都是對於查詢結果進行分頁的。spring
public ResponseMessage<PagePing<DocTemplateDTO>> getDocTemplateList(DocTemplateDTO docTemplateDTO,Integer currentPage,Integer pageSize) { ResponseMessage<PagePing<DocTemplateDTO>> resp = new ResponseMessage<>(); PagePing<DocTemplateDTO> p=new PagePing<>(); try { Page page = (Page) PageHelper.startPage(currentPage,pageSize); DocTemplateE docTemplateE = this.docTemplateDomainFactory.convert(docTemplateDTO); List<DocTemplateE> list = docTemplateE.findList(docTemplateE); if (CollectionUtils.isNotEmpty(list)) { PageInfo<DocTemplateDTO> pageInfo = new PageInfo<>(docTemplateConverter.dataToDTO(list)); // 轉換成本身的分頁信息 PageBean<DocTemplateDTO> pb=new PageBean<>(); pb.setCurrentPage(page.getPageNum()); pb.setPageSize(page.getPageSize()); pb.setPageCount(page.getPages()); pb.setTotalData(page.getTotal()); p.setDataList(pageInfo.getList()); p.setPage(pb); ResponseUtil.setResult(resp, p, ErrorCodeEnum.SUCCESS, true); } else { ResponseUtil.setResult(resp,null, ErrorCodeEnum.NORECORD,false); } } catch (Exception e) { logger.error("查詢單證模板失敗", e); ResponseUtil.setResult(resp,null, ErrorCodeEnum.ERROR,false); } return resp; }
如上。問題坑了2個多小時。記錄一下,望廣大友軍勿採坑sql