SpringBoot使用PageHelper分頁詳解
pagehelper
我們在任何的系統(tǒng)中,分頁功能是必不可少的。然而,對于這個功能如果有一種快速開發(fā)的實現方式,當然可以節(jié)省我們很多的時間了。接下來,我就給大家基于不同的環(huán)境來說說如何使用一個分頁插件:pagehelper,它是Mybatis的一個分頁插件。 這里使用一個簡單的springboot的demo項目來實現,前臺頁面使用的Thymeleaf模板引擎。
首先加入pageHelper的依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency>
然后在配置文件中加入pageHelper的相關配置
# pageHelper配置 # 指定數據庫 pagehelper.helper-dialect=mysql # 頁碼<=0 查詢第一頁,頁碼>=總頁數查詢最后一頁 pagehelper.reasonable=true # 支持通過 Mapper 接口參數來傳遞分頁參數 pagehelper.support-methods-arguments=true
使用
pageHelper就可以使用了。 (基本的springboot的搭建步驟就不說了,之前已經寫過文章) 簡單的去數據庫查一張表,達到分頁效果。
dao層
@Select(value = "select * from address") List<Map> pageListss();
service層和serviceImpl層
List<Map> pageListss( Integer pn);
@Override public List<Map> pageListss(Integer pn) { //判斷的目的是前臺訪問的路徑沒有pn參數,則pn當前頁參數默認為1(第一頁) if(pn==null){ pn=1; } //參數(當前頁,一頁展示多少條) PageHelper.startPage(pn,3); //只有在startPage下面的第一個select動作會被分頁 List<Map> pageList=selectMapper.pageListss(); //把查到的list列表進行pageInfo處理,返回一個分頁列表 PageInfo<Map> pageInfo=new PageInfo(pageList); return pageList; }
controller層
@Controller @RequestMapping(value = "/page") public class PageController { @Autowired SelectService selectService; @RequestMapping("list")//pn是當前頁,頁面?zhèn)鹘o后臺 public String list(Integer pn,Model model){ //這時,從service返回來的列表list已經是被分頁后的列表了 List<Map> list=selectService.pageListss(pn); //把分頁后的list放到model中,在頁面展示信息(thymeleaf模板引擎使用model放置信息) model.addAttribute("list",list); return "test"; } }
test.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>pageHelper練習</title> </head> <body> <table border="1"> <thead> <tr> <th>ID</th> <th>地址</th> <th>詳細地址</th> <th>電話</th> <th>賬號</th> </tr> </thead> <tbody> <tr th:each="user : ${list}"> <!-- 將用戶的主鍵 uId 存在在 name 屬性中--> <td th:text="${user.id}"></td> <td th:text="${user.location}"></td> <!-- 使用dates對象格式化日期--> <td th:text="${user.detail}"></td> <!-- 三運運算判斷是否已婚--> <td th:text="${user.phone}"></td> <td th:text="${user.account}"></td> </tr> </tbody> </table> 當前第 <span th:text="${list.pageNum}"></span> 頁. 總共 <span th:text="${list.pages}"></span> 頁. 一共 <span th:text="${list.total}"></span> 條記錄 <a th:href="@{/page/list?pn=1}" rel="external nofollow" >首頁</a> <a th:href="@{'/page/list?pn='+${list.pageNum-1}}" rel="external nofollow" >上一頁</a> <a th:href="@{'/page/list?pn='+${list.pageNum+1}}" rel="external nofollow" >下一頁</a> <a th:href="@{'/page/list?pn='+${list.pages}}" rel="external nofollow" >尾頁</a> </body> </html>
訪問路徑//localhost:8082/page/list pn參數會默認會1 結果
說明:上面的${list.pageNum},${list.pages},${list.total}等這些屬性都是屬于list的,此時這個list是被分頁后的列表,是從service層傳回來的pageInfo分頁列表。
而這個被pageHelper插件處理后的pageInfo列表具有諸多屬性。
pageInfo類說明(源碼分析)
public class PageInfo<T> implements Serializable { private static final long serialVersionUID = 1L; //當前頁 private int pageNum; //每頁的數量 private int pageSize; //當前頁的數量 private int size; //由于startRow和endRow不常用,這里說個具體的用法 //可以在頁面中"顯示startRow到endRow 共size條數據" //當前頁面第一個元素在數據庫中的行號 private int startRow; //當前頁面最后一個元素在數據庫中的行號 private int endRow; //總記錄數 private long total; //總頁數 private int pages; //結果集 private List<T> list; //前一頁 private int prePage; //下一頁 private int nextPage; //是否為第一頁 private boolean isFirstPage = false; //是否為最后一頁 private boolean isLastPage = false; //是否有前一頁 private boolean hasPreviousPage = false; //是否有下一頁 private boolean hasNextPage = false; //導航頁碼數 private int navigatePages; //所有導航頁號 private int[] navigatepageNums; //導航條上的第一頁 private int navigateFirstPage; //導航條上的最后一頁 private int navigateLastPage; public PageInfo() { } /** * 包裝Page對象 * * @param list */ public PageInfo(List<T> list) { this(list, 8); } /** * 包裝Page對象 * * @param list page結果 * @param navigatePages 頁碼數量 */ public PageInfo(List<T> list, int navigatePages) { if (list instanceof Page) { Page page = (Page) list; this.pageNum = page.getPageNum(); this.pageSize = page.getPageSize(); this.pages = page.getPages(); this.list = page; this.size = page.size(); this.total = page.getTotal(); //由于結果是>startRow的,所以實際的需要+1 if (this.size == 0) { this.startRow = 0; this.endRow = 0; } else { this.startRow = page.getStartRow() + 1; //計算實際的endRow(最后一頁的時候特殊) this.endRow = this.startRow - 1 + this.size; } } else if (list instanceof Collection) { this.pageNum = 1; this.pageSize = list.size(); this.pages = this.pageSize > 0 ? 1 : 0; this.list = list; this.size = list.size(); this.total = list.size(); this.startRow = 0; this.endRow = list.size() > 0 ? list.size() - 1 : 0; } if (list instanceof Collection) { this.navigatePages = navigatePages; //計算導航頁 calcNavigatepageNums(); //計算前后頁,第一頁,最后一頁 calcPage(); //判斷頁面邊界 judgePageBoudary(); } } ....... }
這里只列出所有屬性和構造方法,那么可以清晰的看到一些屬性的含義,一些屬性是如何初始化,并且初始化值是怎樣的,更多詳細情況可以自己去查看源碼。
以上的分頁需求,可以非常方便的使用。 項目經理再也不用擔心我的分頁了!
到此這篇關于SpringBoot使用PageHelper分頁詳解的文章就介紹到這了,更多相關SpringBoot使用PageHelper內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于selenium-java封裝chrome、firefox、phantomjs實現爬蟲
這篇文章主要介紹了基于selenium-java封裝chrome、firefox、phantomjs實現爬蟲,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2020-10-10Springboot jpa @Column命名大小寫問題及解決
這篇文章主要介紹了Springboot jpa @Column命名大小寫問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Spring Boot 中整合 MyBatis-Plus詳細步驟(最新推薦)
本文詳細介紹了如何在SpringBoot項目中整合MyBatis-Plus,包括整合步驟、基本CRUD操作、分頁查詢、批量操作、自定義SQL操作等,通過這些步驟,開發(fā)者可以快速實現數據庫操作,提高開發(fā)效率,感興趣的朋友一起看看吧2025-01-01SpringBoot如何使用MyBatisPlus逆向工程自動生成代碼
本文介紹如何使用SpringBoot、MyBatis-Plus進行逆向工程自動生成代碼,并結合Swagger3.0實現API文檔的自動生成和訪問,通過詳細步驟和配置,確保Swagger與SpringBoot版本兼容,并通過配置文件和測試類實現代碼生成和Swagger文檔的訪問2024-12-12Java?Mybatis?foreach嵌套foreach?List<list<Object>&
在MyBatis的mapper.xml文件中,foreach元素常用于動態(tài)生成SQL查詢條件,此元素包括item(必選,元素別名)、index(可選,元素序號或鍵)、collection(必選,指定迭代對象)、open、separator、close(均為可選,用于定義SQL結構)2024-09-09