使用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 默認從第一頁開始 請求參數(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" >進入視頻列表</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)文章
spring boot整合mybatis+mybatis-plus的示例代碼
這篇文章主要介紹了spring boot整合mybatis+mybatis-plus的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程
這篇文章主要介紹了Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程,ExecutorService可以維護我們的大量線程在操作臨界資源時的穩(wěn)定性。2017-03-03解決RedisTemplate存儲至緩存數(shù)據(jù)出現(xiàn)亂碼的情況
這篇文章主要介紹了解決RedisTemplate存儲至緩存數(shù)據(jù)出現(xiàn)亂碼的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03SpringMVC數(shù)據(jù)響應(yīng)詳細介紹
Spring MVC 是 Spring 提供的一個基于 MVC 設(shè)計模式的輕量級 Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細,本章來講解SpringMVC數(shù)據(jù)響應(yīng)2023-02-02springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認證path
本篇文章主要介紹了springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認證path,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09Springboot整合EasyExcel實現(xiàn)Excel文件上傳方式
這篇文章主要介紹了Springboot整合EasyExcel實現(xiàn)Excel文件上傳方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07Netty分布式pipeline管道傳播事件的邏輯總結(jié)分析
這篇文章主要為大家介紹了Netty分布式pipeline管道傳播事件總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03SpringBoot+Redis實現(xiàn)分布式緩存的方法步驟
在高并發(fā)的分布式的系統(tǒng)中,緩存是提升系統(tǒng)性能的重要手段,本文主要介紹了SpringBoot+Redis實現(xiàn)分布式緩存的方法步驟,具有一定的參考價值,感興趣的可以了解一下2024-07-07