Spring MVC+MyBatis+MySQL實(shí)現(xiàn)分頁功能實(shí)例
前言
最近因?yàn)楣ぷ鞯脑?,在使用SSM框架實(shí)現(xiàn)一個(gè)商品信息展示的功能,商品的數(shù)據(jù)較多,不免用到分頁,查了一番MyBatis分頁的做法,終于是實(shí)現(xiàn)了,在這里記錄下來分享給大家,下面來一起看看詳細(xì)的介紹:
方法如下:
首先寫一個(gè)分頁的工具類,定義當(dāng)前頁數(shù),總頁數(shù),每頁顯示多少等屬性。
/** * 分頁 工具類 */ public class Page implements Serializable { private static final long serialVersionUID = -2213069645383858323L; private int pageNow = 1; // 當(dāng)前頁數(shù) private int pageSize = 4; // 每頁顯示記錄的條數(shù) private int totalCount; // 總的記錄條數(shù) private int totalPageCount; // 總的頁數(shù) private int startPos; // 開始位置,從0開始 private boolean hasFirst;// 是否有首頁 private boolean hasPre;// 是否有前一頁 private boolean hasNext;// 是否有下一頁 private boolean hasLast;// 是否有最后一頁 /** * 通過構(gòu)造函數(shù) 傳入 總記錄數(shù) 和 當(dāng)前頁 * @param totalCount * @param pageNow */ public Page(int totalCount, int pageNow) { this.totalCount = totalCount; this.pageNow = pageNow; } /** * 取得總頁數(shù),總頁數(shù)=總記錄數(shù)/總頁數(shù) * @return */ public int getTotalPageCount() { totalPageCount = getTotalCount() / getPageSize(); return (totalCount % pageSize == 0) ? totalPageCount : totalPageCount + 1; } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public int getPageNow() { return pageNow; } public void setPageNow(int pageNow) { this.pageNow = pageNow; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } /** * 取得選擇記錄的初始位置 * @return */ public int getStartPos() { return (pageNow - 1) * pageSize; } public void setStartPos(int startPos) { this.startPos = startPos; } /** * 是否是第一頁 * @return */ public boolean isHasFirst() { return (pageNow == 1) ? false : true; } public void setHasFirst(boolean hasFirst) { this.hasFirst = hasFirst; } /** * 是否有首頁 * @return */ public boolean isHasPre() { // 如果有首頁就有前一頁,因?yàn)橛惺醉摼筒皇堑谝豁? return isHasFirst() ? true : false; } public void setHasPre(boolean hasPre) { this.hasPre = hasPre; } /** * 是否有下一頁 * @return */ public boolean isHasNext() { // 如果有尾頁就有下一頁,因?yàn)橛形岔摫砻鞑皇亲詈笠豁? return isHasLast() ? true : false; } public void setHasNext(boolean hasNext) { this.hasNext = hasNext; } /** * 是否有尾頁 * @return */ public boolean isHasLast() { // 如果不是最后一頁就有尾頁 return (pageNow == getTotalCount()) ? false : true; } public void setHasLast(boolean hasLast) { this.hasLast = hasLast; } }
接著Mapper接口中定義分類的方法
傳入兩個(gè)參數(shù),分別是開始頁和每頁顯示記錄的條數(shù)。
Mapper的映射文件中的SQL分頁語句
<select id="findPages" resultType="com.dh15.pojo.Goods"> select g.id,g.name,g.price,g.num,c.class_name,g.pic,g.des from tb_goods g,tb_class c where g.class_id=c.cid limit #{startPos},#{pageSize} </select>
接著在控制類(Controller)中查詢商品,同時(shí)進(jìn)行分頁。
查詢商品方法
/** * 查詢商品信息,實(shí)現(xiàn)分頁 * @param goods * @return * @throws Exception */ @RequestMapping("/queryPages") public String queryPages(HttpServletRequest request, Model model) throws Exception { String pageNow = request.getParameter("pageNow"); Page page = null; List<Goods> goods = new ArrayList<Goods>(); int totalCount = (int) service.getGoodsCount(1); if (pageNow != null) { page = new Page(totalCount, Integer.parseInt(pageNow)); goods = this.service.findPages(page.getStartPos(), page.getPageSize()); } else { page = new Page(totalCount, 1); goods = this.service.findPages(page.getStartPos(), page.getPageSize()); } model.addAttribute("goods_list", goods); model.addAttribute("page", page); return "goods/FenYeTest"; }
這里需要注意一下,遍歷商品時(shí)沒有點(diǎn)擊頁數(shù),所以當(dāng)前頁默認(rèn)是空的,這里要在方法里面進(jìn)行判斷,如果pageNow為空,傳一個(gè)1到構(gòu)造函數(shù),否則會報(bào)空指針。
最后,在jsp頁面顯示查詢的商品列表,同時(shí)實(shí)現(xiàn)點(diǎn)擊上一頁,下一頁,首頁,尾頁等常見分頁中的操作
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>商品查詢列表</title> </head> <body> <table width="100%" border=1> <tr> <td>選擇</td> <td>商品名稱</td> <td>商品價(jià)格</td> <td>生產(chǎn)數(shù)量</td> <td>商品類別</td> <td>商品圖片</td> <td>商品信息</td> <td>操作</td> </tr> <c:forEach items="${goods_list }" var="item"> <tr> <td>${item.id}<input type="hidden" name="id" value="${item.id}" /></td> <td>${item.name }</td> <td>${item.price }</td> <td>${item.num }</td> <td>${item.classInfo.class_name }</td> <td><img style="width:110px;height:120px" alt="商品圖片" src="http://localhost/pic/${item.pic }"></td> <td>${item.des }</td> <td> <a href="${pageContext.request.contextPath }/findGoodsById.action?id=${item.id}" rel="external nofollow" >修改</a> <a href="${pageContext.request.contextPath }/delGoods.action?id=${item.id}" rel="external nofollow" >刪除</a> </td> </tr> </c:forEach> </table> <div align="center"> <font size="2">第 ${page.pageNow} 頁</font> <a href="queryPages.action?pageNow=1" rel="external nofollow" rel="external nofollow" >首頁</a> <c:choose> <c:when test="${page.pageNow - 1 > 0}"> <a href="queryPages.action?pageNow=${page.pageNow - 1}" rel="external nofollow" >上一頁</a> </c:when> <c:when test="${page.pageNow - 1 <= 0}"> <a href="queryPages.action?pageNow=1" rel="external nofollow" rel="external nofollow" >上一頁</a> </c:when> </c:choose> <c:choose> <c:when test="${page.totalPageCount==0}"> <a href="queryPages.action?pageNow=${page.pageNow}" rel="external nofollow" rel="external nofollow" >下一頁</a> </c:when> <c:when test="${page.pageNow + 1 < page.totalPageCount}"> <a href="queryPages.action?pageNow=${page.pageNow + 1}" rel="external nofollow" >下一頁</a> </c:when> <c:when test="${page.pageNow + 1 >= page.totalPageCount}"> <a href="queryPages.action?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >下一頁</a> </c:when> </c:choose> <c:choose> <c:when test="${page.totalPageCount==0}"> <a href="queryPages.action?pageNow=${page.pageNow}" rel="external nofollow" rel="external nofollow" >尾頁</a> </c:when> <c:otherwise> <a href="queryPages.action?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >尾頁</a> </c:otherwise> </c:choose> </div> </body> </html>
最終效果圖:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Mybatis分頁插件PageHelper的使用詳解
- Java的MyBatis框架中實(shí)現(xiàn)多表連接查詢和查詢結(jié)果分頁
- Mybatis-Plus 多表聯(lián)查分頁的實(shí)現(xiàn)代碼
- Java簡單實(shí)現(xiàn)SpringMVC+MyBatis分頁插件
- SpringMvc+Mybatis+Pagehelper分頁詳解
- Mybatis-plus新版本分頁失效PaginationInterceptor過時(shí)的問題
- Springboot中MyBatisplus使用IPage和Page分頁的實(shí)例代碼
- 完美解決MybatisPlus插件分頁查詢不起作用總是查詢?nèi)繑?shù)據(jù)問題
- MyBatisPlus分頁的同時(shí)指定排序規(guī)則說明
- mybatis分頁插件pageHelper詳解及簡單實(shí)例
- MyBatis-Plus 分頁查詢以及自定義sql分頁的實(shí)現(xiàn)
- Java使用MyBatis框架分頁的5種方式
- mybatis-plus分頁傳入?yún)?shù)后sql where條件沒有l(wèi)imit分頁信息操作
- mybatis-plus分頁查詢的實(shí)現(xiàn)示例
- MyBatis-Plus分頁插件不生效的解決方法
- SpringBoot 使用Mybatis分頁插件實(shí)現(xiàn)詳解
- Mybatis實(shí)現(xiàn)增刪改查及分頁查詢的方法
- Mybatis分頁的4種方式實(shí)例
相關(guān)文章
Mybatis執(zhí)行SQL時(shí)多了一個(gè)limit的問題及解決方法
這篇文章主要介紹了Mybatis執(zhí)行SQL時(shí)多了一個(gè)limit的問題及解決方法,Mybatis攔截器方法識別到配置中參數(shù)supportMethodsArguments 為ture時(shí)會分頁處理,本文結(jié)合示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-10-10使用JAVA實(shí)現(xiàn)郵件發(fā)送功能的圖文教程
郵件發(fā)送其實(shí)是一個(gè)非常常見的需求,用戶注冊,找回密碼等地方,都會用到,下面這篇文章主要給大家介紹了關(guān)于使用JAVA實(shí)現(xiàn)郵件發(fā)送功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Java實(shí)現(xiàn)局域網(wǎng)聊天小程序
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)局域網(wǎng)聊天小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Java基本類型包裝類概述與Integer類、Character類用法分析
這篇文章主要介紹了Java基本類型包裝類概述與Integer類、Character類用法,結(jié)合實(shí)例形式分析了java基本數(shù)據(jù)類型與字符串轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2019-03-03spring基礎(chǔ)概念A(yù)OP與動(dòng)態(tài)代理理解
這篇文章主要為大家詳細(xì)介紹了spring基礎(chǔ)概念A(yù)OP與動(dòng)態(tài)代理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10SpringBoot項(xiàng)目不占用端口啟動(dòng)的方法
這篇文章主要介紹了SpringBoot項(xiàng)目不占用端口啟動(dòng)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08java核心編程之文件過濾類FileFilter和FilenameFilter
這篇文章主要為大家詳細(xì)介紹了java文件過濾類FileFilter和FilenameFilter,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08