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