以前在spring+springmvc因爲整個大多都是xml的配置,在使用spring-boot後,須要進行分頁,
也但願可以減小xml的配置以及新建不少分頁的相關類,找到了pageHelper這個插件,
分頁起來很是方便mysql
新建一個page< T > 用來接收分頁信息git
/** * @Author xuelongjiang */ public class Page<T> { private Integer pageNo = 0; private Integer pageSize = 10; private T t; public Integer getPageNo() { return pageNo; } public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public T getT() { return t; } public void setT(T t) { this.t = t; } }
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
這裏使用的是yml(可讀性很強,也能少敲些鍵盤,愛護一下咱們可愛的鍵盤^-^)的方式,github
pagehelper: helperDialect: mysql reasonable: true
public PageInfo<AnswerQuestionDTO> answerAndQuestionDetailByPage(Page<AnswerQuestionDTO> page){//使用page保包裝咱們的類 PageHelper.startPage(page.getPageNo(),page.getPageSize());//設置分頁相關值 List<AnswerQuestionDTO> answerQuestionDTOList = answerAndQuestionDao.answerAndQuestionDetailList(page.getT());//查詢 PageInfo<AnswerQuestionDTO> pageInfo = new PageInfo<>(answerQuestionDTOList);//包裝爲分頁結果 return pageInfo; }
關注個人公衆號第一時間閱讀有趣的技術故事
掃碼關注:spring
也能夠在微信搜索公衆號便可關注我:codexiulian
渴望與你一塊兒成長進步!sql