springboot使用PageHelper实现分页
生活随笔
收集整理的這篇文章主要介紹了
springboot使用PageHelper实现分页
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用mybatis最頭疼的就是寫(xiě)分頁(yè),需要先寫(xiě)一個(gè)查詢(xún)count的select語(yǔ)句,在寫(xiě)一個(gè)真正的limit查詢(xún)語(yǔ)句,所以花費(fèi)很長(zhǎng)的時(shí)間,這里咋們可以使用PageHelper實(shí)現(xiàn)分頁(yè)。
1.首先引入pom依賴(lài):
<!-- springboot分頁(yè)插件 --> <dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.4</version> </dependency>?2.1在application.properties中添加分頁(yè)配置:
# 配置pageHelper分頁(yè)插件的內(nèi)容
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
??2.2或者在application.yml文件中添加分頁(yè)配置:
pagehelper:
?helperDialect: mysql
?reasonable: true
?supportMethodsArguments: true
?params: count=countSql
3.最后在controller或者service中如下寫(xiě)法:
@RequestMapping("/PageHelpe")@ResponseBodypublic PageInfo<fwl> PageHelpe(){//使用PageHelper設(shè)置分頁(yè)----1是當(dāng)前頁(yè)3是顯示條數(shù)PageHelper.startPage(1,3);PageInfo<fwl> pageInfo = new PageInfo<>(fwlmapper.selectall());return pageInfo;}注意:雖然fwlmapper.selectall()是查詢(xún)所有數(shù)據(jù),但是因?yàn)橐呀?jīng)配置了pagehelper,所以并不需要寫(xiě)分頁(yè)sql,分頁(yè)插件會(huì)攔截查詢(xún)請(qǐng)求,并讀取前臺(tái)傳來(lái)的分頁(yè)查詢(xún)參數(shù)重新生成分頁(yè)查詢(xún)語(yǔ)句,所以實(shí)際上不會(huì)查詢(xún)所有的數(shù)據(jù)。只會(huì)查當(dāng)前頁(yè)的相關(guān)數(shù)據(jù)。?
顯示效果如下:
{"pageNum": 1,"pageSize": 3,"size": 3,"startRow": 1,"endRow": 3,"total": 16,"pages": 6,"list": [{"id": 1,"fwl": 41247,"time": "2019-03-19"},{"id": 2,"fwl": 41248,"time": "2019-03-20"},{"id": 3,"fwl": 41248,"time": "2019-03-21"}],"prePage": 0,"nextPage": 2,"isFirstPage": true,"isLastPage": false,"hasPreviousPage": false,"hasNextPage": true,"navigatePages": 8,"navigatepageNums": [1,2,3,4,5,6],"navigateFirstPage": 1,"navigateLastPage": 6,"lastPage": 6,"firstPage": 1 }前臺(tái)分頁(yè)的話(huà)可以用分頁(yè)插件:Bootstrap的分頁(yè)插件? ?pagination
大概就是新建一個(gè)列表。
<ul class="pagination"></ul>引入?pagination的js,然后寫(xiě)js就行了,js大概就是這樣
$(function(){$('#pagination').bootstrapPaginator({bootstrapMajorVersion:3,currentPage:${pageInfo.pageNum},totalPages:${pageInfo.pages},pageUrl:function(type,page, current){return '${pageContext.request.contextPath}/backend/productType/findAll?pageNum='+page;},itemTexts: function (type, page, current) {switch (type) {case "first":return "首頁(yè)";case "prev":return "上一頁(yè)";case "next":return "下一頁(yè)";case "last":return "末頁(yè)";case "page":return page;}}});});?
總結(jié)
以上是生活随笔為你收集整理的springboot使用PageHelper实现分页的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。