您好,登錄后才能下訂單哦!
本篇內容主要講解“JavaWeb分頁查詢功能怎么實現”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“JavaWeb分頁查詢功能怎么實現”吧!
效果:
實現:
分頁查詢有幾個比較重要的參數,pageNum當前的頁數,pageSize每頁所展示的數據,totalRecord總共有多少數據,其他的數據可以根據這三個數據得出,有了這幾個數據就可以分頁查詢了
public class Page { //當前的頁數 private int pageNum; //每頁所展示的數據 private int pageSize; //總共有多少數據 private int totalRecord; //總頁數 private int totalPage; //查詢數據的起始位置 private int startIndex; //存放查詢到的數據 private List<Book> booklist; //相對的頭頁 private int start; //相對的尾頁 private int end; public Page(int pageNum, int pageSize, int totalRecord) { this.pageNum = pageNum; this.pageSize = pageSize; this.totalRecord = totalRecord; if(totalRecord%pageSize == 0){ //如果剛好能夠整除,則totalPage = totalRecord/pageSize this.totalPage = totalRecord/pageSize; }else{ //如歌不能整除,則需要多加一頁顯示余下的數據 this.totalPage = (totalRecord/pageSize)+1; } this.startIndex = (pageNum-1)*pageSize; //初始頭頁 this.start = 1; //初始尾頁 this.end = 5; if(totalPage<5){ //如果總頁數小于5頁,則尾頁為總頁數 this.end = this.totalPage; }else{ this.start = this.pageNum-2; this.end = this.pageNum+2; if(this.start<0){ this.start = 1; this.end = 5; } if(this.end>totalPage){ this.end = totalPage; this.start = this.end-5; } } } //下面的為get/set方法 public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getStartIndex() { return startIndex; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } public List<Book> getBooklist() { return booklist; } public void setBooklist(List<Book> booklist) { this.booklist = booklist; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } }
pageNum從請求中獲得,pageSize自己設置,totalRecord通過mybatis查詢獲得,一開始登錄的時候請求中沒有pageNum,所以進行一個判斷,如果沒有pageNum則賦值為1
//pageNum String pageNum = request.getParameter("pageNum") == null?"1":request.getParameter("pageNum");//pageSize int pageSize = 7;//totalRecordint totalRecord = bookMapper.BookSize();//mybatis<select id="BookSize" resultType="int"> select count(*) from book </select>
有了pageNum和startIndex就可以查詢到特定范圍的數據,通過使用mysql中的limit函數,limit有兩個參數,第一個參數為起始位置,即從那開始查,第二個參數為查詢范圍,即查多少個,如 limit 14,7 為從第14個數據開始往后查七個。
<select id="Books" parameterType="java.util.Map" resultType="book"> select *from book limit #{startIndex},#{pageSize} </select>
public static Page getPageUser(int pageNum,int pageSize){ SqlSession sqlSession = GetSqlSession.getSqlSession(); BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); int totalRecord = bookMapper.BookSize(); Page page = new Page(pageNum,pageSize ,totalRecord ); int startIndex = page.getStartIndex(); Map<String,Integer> map = new HashMap<>(); map.put("startIndex",startIndex ); map.put("pageSize",pageSize ); page.setBooklist(bookMapper.Books(map)); return page; }
jsp頁面:
<a href="index?pageNum=1">首頁</a> <%--當前頁數為第一頁--%> <c:if test="${sessionScope.page.pageNum == 1}"> <c:forEach begin="${sessionScope.page.start}" end="${sessionScope.page.end}" step="1" var="i"> <c:if test="${sessionScope.page.pageNum == i}"> <span style="color: deeppink">${i}</span> </c:if> <c:if test="${sessionScope.page.pageNum != i}"> <a href="index?pageNum=${i}">${i}</a> </c:if> </c:forEach> <a href="index?pageNum=${sessionScope.page.pageNum+1}">下一頁</a> </c:if> <%--當前頁數為中間頁數--%> <c:if test="${sessionScope.page.pageNum >1 && sessionScope.page.pageNum<sessionScope.page.totalPage}"> <a href="index?pageNum=${sessionScope.page.pageNum-1}">上一頁</a> <c:forEach begin="${sessionScope.page.start}" end="${sessionScope.page.end}" step="1" var="i"> <c:if test="${sessionScope.page.pageNum == i}"> <span style="color: deeppink">${i}</span> </c:if> <c:if test="${sessionScope.page.pageNum != i}"> <a href="index?pageNum=${i}">${i}</a> </c:if> </c:forEach> <a href="index?pageNum=${sessionScope.page.pageNum+1}">下一頁</a> </c:if> <%--當前頁數為最后一頁--%> <c:if test="${sessionScope.page.pageNum == sessionScope.page.totalPage}"> <a href="index?pageNum=${sessionScope.page.pageNum-1}">上一頁</a> <c:forEach begin="${sessionScope.page.start}" end="${sessionScope.page.end}" step="1" var="i"> <c:if test="${sessionScope.page.pageNum == i}"> <span style="color: deeppink">${i}</span> </c:if> <c:if test="${sessionScope.page.pageNum != i}"> <a href="index?pageNum=${i}">${i}</a> </c:if> </c:forEach> </c:if> <a href="index?pageNum=${sessionScope.page.totalPage}">尾頁</a>
到此,相信大家對“JavaWeb分頁查詢功能怎么實現”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。