springboot使用PageHelper实现分页
生活随笔
收集整理的這篇文章主要介紹了
springboot使用PageHelper实现分页
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用mybatis最頭疼的就是寫分頁,需要先寫一個查詢count的select語句,在寫一個真正的limit查詢語句,所以花費很長的時間,這里咋們可以使用PageHelper實現(xiàn)分頁。
1.首先引入pom依賴:
<!-- springboot分頁插件 --> <dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.4</version> </dependency>?2.1在application.properties中添加分頁配置:
# 配置pageHelper分頁插件的內(nèi)容
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
??2.2或者在application.yml文件中添加分頁配置:
pagehelper:
?helperDialect: mysql
?reasonable: true
?supportMethodsArguments: true
?params: count=countSql
3.最后在controller或者service中如下寫法:
@RequestMapping("/PageHelpe")@ResponseBodypublic PageInfo<fwl> PageHelpe(){//使用PageHelper設置分頁----1是當前頁3是顯示條數(shù)PageHelper.startPage(1,3);PageInfo<fwl> pageInfo = new PageInfo<>(fwlmapper.selectall());return pageInfo;}注意:雖然fwlmapper.selectall()是查詢所有數(shù)據(jù),但是因為已經(jīng)配置了pagehelper,所以并不需要寫分頁sql,分頁插件會攔截查詢請求,并讀取前臺傳來的分頁查詢參數(shù)重新生成分頁查詢語句,所以實際上不會查詢所有的數(shù)據(jù)。只會查當前頁的相關數(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 }前臺分頁的話可以用分頁插件:Bootstrap的分頁插件? ?pagination
大概就是新建一個列表。
<ul class="pagination"></ul>引入?pagination的js,然后寫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 "首頁";case "prev":return "上一頁";case "next":return "下一頁";case "last":return "末頁";case "page":return page;}}});});?
總結
以上是生活随笔為你收集整理的springboot使用PageHelper实现分页的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idr是什么货币
- 下一篇: Docker(二)基本操作命令