使用mybatis插件PageHelper實現(xiàn)分頁效果
最近都在忙著寫一個網(wǎng)站項目,今天做一個分頁功能的時候,遇到了分頁效果實現(xiàn)不了的問題,查了好久的資料,后來終于是成功解決啦,記錄一下
1.在pom.xml中添加分頁插件依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.5</version> </dependency>
2.在mybatis配置文件中配置分頁插件
這里需要注意的是,如果你的項目有mybatis的配置文件時,添加下面配置:(配置參數(shù)可根據(jù)需要添加或刪除)
<plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> <property name="offsetAsPageNum" value="false"/> <property name="rowBoundsWithCount" value="false"/> <property name="pageSizeZero" value="true"/> <property name="reasonable" value="false"/> <property name="supportMethodsArguments" value="false"/> <property name="returnPageInfo" value="none"/> </plugin> </plugins>
但如果你的項目沒有單獨配置mybatis的配置文件,而是把spring和mybatis的配置結(jié)合起來的話,這時候你需要引入如下配置信息:
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/wang/web/mapper/*.xml"></property>
<!-- 配置分頁插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
3.controller層
//訪問所有視頻信息查詢頁面
/**
* 分頁查詢所有視頻信息
* @param pn 默認(rèn)從第一頁開始 請求參數(shù)
* @return
*/
@RequestMapping("/ShowMedia")
public String Show(@RequestParam(required = false,value="pn",defaultValue="1")Integer pn, HttpServletRequest request){
TbMediaExample example = new TbMediaExample();
//從第一條開始 每頁查詢五條數(shù)據(jù)
PageHelper.startPage(pn, 5);
List<TbMedia> mediaList = mediaService.selectByExample(example);
//將用戶信息放入PageInfo對象里
PageInfo pageInfo = new PageInfo(mediaList,5);
System.out.println(pageInfo.getPages());
request.setAttribute("pageInfo", pageInfo);
return "/media";
}
4.前臺
<div class="result-content">
<table class="result-tab" width="100%">
<tr>
<th class="tc" width="5%"><input class="allChoose" name="" type="checkbox"></th>
<th>排序</th>
<th>ID</th>
<th>視頻標(biāo)題</th>
<th>視頻資源</th>
<th>視頻圖片</th>
<th>視頻描述</th>
<th>上傳時間</th>
<th>操作</th>
</tr>
<c:if test="${!empty pageInfo.list }">
<c:forEach items="${pageInfo.list}" var="media">
<tr>
<td class="tc"><input name="id[]" value="59" type="checkbox"></td>
<td>
<input name="ids[]" value="59" type="hidden">
<input class="common-input sort-input" name="ord[]" value="0" type="text">
</td>
<td align="center">${media.id }</td>
<td align="center">${media.title }</td>
<td align="center">${media.src }</td>
<td align="center">${media.picture }</td>
<td align="center">${media.descript }</td>
<td align="center">${media.uptime }</td>
<td>
<a class="link-update" href="<%=basePath%>user/MediaUpdate?id=${media.id }" rel="external nofollow" >修改</a>
<a class="link-del" href="<%=basePath%>user/MediaList" rel="external nofollow" >進(jìn)入視頻列表</a>
<a class="link-del" href="javascript:del('${media.id }')" rel="external nofollow" >刪除視頻</a>
</td>
</tr>
</c:forEach>
</c:if>
</table>
<hr style="height:1px;border:none;border-top:1px solid #ccc;" />
<!-- 分頁導(dǎo)航欄 -->
<!-- 分頁信息 -->
<div class="row">
<!-- 分頁文字信息,其中分頁信息都封裝在pageInfo中 -->
<div class="col-md-6">
當(dāng)前第:${pageInfo.pageNum}頁,總共:${pageInfo.pages}頁,總共:${pageInfo.total}條記錄
</div>
<!-- 分頁條 -->
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="<%=basePath%>user/ShowMedia?pn=1" rel="external nofollow" >首頁</a></li>
<c:if test="${pageInfo.hasPreviousPage }">
<li>
<a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pageNum-1}" rel="external nofollow" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
<c:if test="${page_Num == pageInfo.pageNum }">
<li class="active"><a href="#" rel="external nofollow" >${ page_Num}</a></li>
</c:if>
<c:if test="${page_Num != pageInfo.pageNum }">
<li><a href="<%=basePath%>user/ShowMedia?pn=${ page_Num}" rel="external nofollow" >${ page_Num}</a></li>
</c:if>
</c:forEach>
<c:if test="${pageInfo.hasNextPage }">
<li>
<a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pageNum+1}" rel="external nofollow" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<li><a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pages}" rel="external nofollow" >末頁</a></li>
</ul>
</nav>
</div>
</div>
效果實現(xiàn)如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Mybatis分頁插件PageHelper的使用詳解
- SpringBoot項目中分頁插件PageHelper無效的問題及解決方法
- SpringMvc+Mybatis+Pagehelper分頁詳解
- mybatis分頁插件pageHelper詳解及簡單實例
- PageHelper插件實現(xiàn)一對多查詢時的分頁問題
- Spring Boot+Mybatis+Druid+PageHelper實現(xiàn)多數(shù)據(jù)源并分頁的方法
- Springboot整合pagehelper分頁功能
- Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)
- SpringBoot集成MyBatis的分頁插件PageHelper實例代碼
- 使用PageHelper插件實現(xiàn)Service層分頁
相關(guān)文章
Java報錯:java.util.concurrent.ExecutionException的解決辦法
在Java并發(fā)編程中,我們經(jīng)常使用java.util.concurrent包提供的工具來管理和協(xié)調(diào)多個線程的執(zhí)行,va并發(fā)編程中,然而,在使用這些工具時,可能會遇到各種各樣的異常,其中之一就是java.util.concurrent.ExecutionException,本文將詳細(xì)分析這種異常的背景、可能的原因2024-09-09
Java實現(xiàn)時間與字符串互相轉(zhuǎn)換詳解
這篇文章主要為大家詳細(xì)介紹了Java中實現(xiàn)時間與字符串互相轉(zhuǎn)換的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Java?常量池詳解之class文件常量池?和class運行時常量池
這篇文章主要介紹了Java?常量池詳解之class文件常量池?和class運行時常量池,常量池主要存放兩大類常量:字面量,符號引用,本文結(jié)合示例代碼對java class常量池相關(guān)知識介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Java案例使用比較排序器comparator實現(xiàn)成績排序
這篇文章主要介紹了Java案例使用比較排序器comparator實現(xiàn)成績排序,主要通過案例用TreeSet集合存儲多個學(xué)生信息,并遍歷該集合,要按照總分從高到低進(jìn)行排序,下文介紹需要的朋友可以參考一下2022-04-04

