SpringBoot使用PageHelper分頁(yè)詳解
pagehelper
我們?cè)谌魏蔚南到y(tǒng)中,分頁(yè)功能是必不可少的。然而,對(duì)于這個(gè)功能如果有一種快速開(kāi)發(fā)的實(shí)現(xiàn)方式,當(dāng)然可以節(jié)省我們很多的時(shí)間了。接下來(lái),我就給大家基于不同的環(huán)境來(lái)說(shuō)說(shuō)如何使用一個(gè)分頁(yè)插件:pagehelper,它是Mybatis的一個(gè)分頁(yè)插件。 這里使用一個(gè)簡(jiǎn)單的springboot的demo項(xiàng)目來(lái)實(shí)現(xiàn),前臺(tái)頁(yè)面使用的Thymeleaf模板引擎。
首先加入pageHelper的依賴(lài)
<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 # 頁(yè)碼<=0 查詢(xún)第一頁(yè),頁(yè)碼>=總頁(yè)數(shù)查詢(xún)最后一頁(yè) pagehelper.reasonable=true # 支持通過(guò) Mapper 接口參數(shù)來(lái)傳遞分頁(yè)參數(shù) pagehelper.support-methods-arguments=true
使用
pageHelper就可以使用了。 (基本的springboot的搭建步驟就不說(shuō)了,之前已經(jīng)寫(xiě)過(guò)文章) 簡(jiǎn)單的去數(shù)據(jù)庫(kù)查一張表,達(dá)到分頁(yè)效果。
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)訪問(wèn)的路徑?jīng)]有pn參數(shù),則pn當(dāng)前頁(yè)參數(shù)默認(rèn)為1(第一頁(yè))
if(pn==null){
pn=1;
}
//參數(shù)(當(dāng)前頁(yè),一頁(yè)展示多少條)
PageHelper.startPage(pn,3);
//只有在startPage下面的第一個(gè)select動(dòng)作會(huì)被分頁(yè)
List<Map> pageList=selectMapper.pageListss();
//把查到的list列表進(jìn)行pageInfo處理,返回一個(gè)分頁(yè)列表
PageInfo<Map> pageInfo=new PageInfo(pageList);
return pageList;
}
controller層
@Controller
@RequestMapping(value = "/page")
public class PageController {
@Autowired
SelectService selectService;
@RequestMapping("list")//pn是當(dāng)前頁(yè),頁(yè)面?zhèn)鹘o后臺(tái)
public String list(Integer pn,Model model){
//這時(shí),從service返回來(lái)的列表list已經(jīng)是被分頁(yè)后的列表了
List<Map> list=selectService.pageListss(pn);
//把分頁(yè)后的list放到model中,在頁(yè)面展示信息(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}">
<!-- 將用戶(hù)的主鍵 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> 頁(yè).
總共 <span th:text="${list.pages}"></span> 頁(yè).
一共 <span th:text="${list.total}"></span> 條記錄
<a th:href="@{/page/list?pn=1}" rel="external nofollow" >首頁(yè)</a>
<a th:href="@{'/page/list?pn='+${list.pageNum-1}}" rel="external nofollow" >上一頁(yè)</a>
<a th:href="@{'/page/list?pn='+${list.pageNum+1}}" rel="external nofollow" >下一頁(yè)</a>
<a th:href="@{'/page/list?pn='+${list.pages}}" rel="external nofollow" >尾頁(yè)</a>
</body>
</html>
訪問(wèn)路徑//localhost:8082/page/list pn參數(shù)會(huì)默認(rèn)會(huì)1 結(jié)果

說(shuō)明:上面的${list.pageNum},${list.pages},${list.total}等這些屬性都是屬于list的,此時(shí)這個(gè)list是被分頁(yè)后的列表,是從service層傳回來(lái)的pageInfo分頁(yè)列表。

而這個(gè)被pageHelper插件處理后的pageInfo列表具有諸多屬性。
pageInfo類(lèi)說(shuō)明(源碼分析)
public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
//當(dāng)前頁(yè)
private int pageNum;
//每頁(yè)的數(shù)量
private int pageSize;
//當(dāng)前頁(yè)的數(shù)量
private int size;
//由于startRow和endRow不常用,這里說(shuō)個(gè)具體的用法
//可以在頁(yè)面中"顯示startRow到endRow 共size條數(shù)據(jù)"
//當(dāng)前頁(yè)面第一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
private int startRow;
//當(dāng)前頁(yè)面最后一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
private int endRow;
//總記錄數(shù)
private long total;
//總頁(yè)數(shù)
private int pages;
//結(jié)果集
private List<T> list;
//前一頁(yè)
private int prePage;
//下一頁(yè)
private int nextPage;
//是否為第一頁(yè)
private boolean isFirstPage = false;
//是否為最后一頁(yè)
private boolean isLastPage = false;
//是否有前一頁(yè)
private boolean hasPreviousPage = false;
//是否有下一頁(yè)
private boolean hasNextPage = false;
//導(dǎo)航頁(yè)碼數(shù)
private int navigatePages;
//所有導(dǎo)航頁(yè)號(hào)
private int[] navigatepageNums;
//導(dǎo)航條上的第一頁(yè)
private int navigateFirstPage;
//導(dǎo)航條上的最后一頁(yè)
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 頁(yè)碼數(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(最后一頁(yè)的時(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)航頁(yè)
calcNavigatepageNums();
//計(jì)算前后頁(yè),第一頁(yè),最后一頁(yè)
calcPage();
//判斷頁(yè)面邊界
judgePageBoudary();
}
}
.......
}
這里只列出所有屬性和構(gòu)造方法,那么可以清晰的看到一些屬性的含義,一些屬性是如何初始化,并且初始化值是怎樣的,更多詳細(xì)情況可以自己去查看源碼。
以上的分頁(yè)需求,可以非常方便的使用。 項(xiàng)目經(jīng)理再也不用擔(dān)心我的分頁(yè)了!
到此這篇關(guān)于SpringBoot使用PageHelper分頁(yè)詳解的文章就介紹到這了,更多相關(guān)SpringBoot使用PageHelper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java讀寫(xiě)文件,在文件中搜索內(nèi)容,并輸出含有該內(nèi)容的所有行方式
這篇文章主要介紹了Java讀寫(xiě)文件,在文件中搜索內(nèi)容,并輸出含有該內(nèi)容的所有行方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
又一波Java專(zhuān)業(yè)人士必備書(shū)籍來(lái)襲
又一波Java專(zhuān)業(yè)人士必備書(shū)籍來(lái)襲,這篇文章主要向大家推薦了Java專(zhuān)業(yè)人士必讀的書(shū),感興趣的小伙伴們不要錯(cuò)過(guò)2016-09-09
springboot?vue項(xiàng)目管理后端實(shí)現(xiàn)接口新增
這篇文章主要為大家介紹了springboot?vue項(xiàng)目管理后端實(shí)現(xiàn)接口新增,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
詳解SpringSecurity中的Authentication信息與登錄流程
這篇文章主要介紹了SpringSecurity中的Authentication信息與登錄流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java 實(shí)現(xiàn)Excel文檔添加超鏈接的代碼
超鏈接即內(nèi)容鏈接,通過(guò)給特定對(duì)象設(shè)置超鏈接,可實(shí)現(xiàn)載體與特定網(wǎng)頁(yè)、文件、郵件、網(wǎng)絡(luò)等的鏈接,點(diǎn)擊鏈接載體可打開(kāi)鏈接目標(biāo),在文檔處理中是一種比較常用的功能,本文將介紹通過(guò)Java程序給Excel文檔添加超鏈接的方法,感興趣的朋友一起看看吧2020-02-02
使用lombok@Data啟動(dòng)項(xiàng)目報(bào)錯(cuò)問(wèn)題及解決
在使用Lombok時(shí),可能會(huì)遇到實(shí)體類(lèi)中的@Data注解不生效,導(dǎo)致get方法找不到的問(wèn)題,解決這一問(wèn)題通常需要三個(gè)步驟:首先,檢查項(xiàng)目設(shè)置中編譯規(guī)則是否勾選;其次,確認(rèn)IDE中是否安裝了Lombok插件2024-10-10
獲取Java線程轉(zhuǎn)儲(chǔ)的常用方法(推薦)
這篇文章主要介紹了獲取Java線程轉(zhuǎn)儲(chǔ)的常用方法,本文給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java使用itext5實(shí)現(xiàn)生成多個(gè)PDF并合并
這篇文章主要為大家詳細(xì)介紹了Java如何使用itext5實(shí)現(xiàn)生成多個(gè)PDF并合并,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04

