您好,登錄后才能下訂單哦!
這篇文章主要介紹“SSM框架分頁如何實現(xiàn)”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SSM框架分頁如何實現(xiàn)”文章能幫助大家解決問題。
1.首先創(chuàng)建一個分頁的工具類
package cn.page.po; import java.io.Serializable; public class Page implements Serializable { private static final long serialVersionUID = -3198048449643774660L; private int pageNow = 1; // 當前頁數(shù) private int pageSize = 4; // 每頁顯示記錄的條數(shù) private int totalCount; // 總的記錄條數(shù) private int totalPageCount; // 總的頁數(shù) @SuppressWarnings("unused") private int startPos; // 開始位置,從0開始 /** * 通過構(gòu)造函數(shù) 傳入 總記錄數(shù) 和 當前頁 * @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 //總頁數(shù) : 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; } }
2.寫mapper.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.page.mapper.UserMapper"><resultMap type="cn.page.po.User" id="userMap"> <id property="id" column="id" /> <result property="emplnumb" column="emplnumb" /> <result property="username" column="username" /> <result property="password" column="password" /> <result property="address" column="address" /> </resultMap> <!-- 分頁SQL語句 --> <select id="selectUserByPage" resultMap="userMap"> select * from user limit #{startPos},#{pageSize} </select> <!-- 取得記錄的總數(shù) --> <select id="getUserCount" resultType="long"> SELECT COUNT(*) FROM user </select></mapper>
3.mapper接口
/** * 使用注解方式傳入多個參數(shù) * @param page * @return startPos},#{pageSize} */public List<User> selectUserByPage(@Param(value="startPos") Integer startPos, @Param(value="pageSize") Integer pageSize); /** * 用戶總數(shù) */public long getUserCount();
4.service接口與實現(xiàn)類
void showUserByPage(HttpServletRequest request,Model model); @Autowiredprivate UserMapper userMapper; @Override public void showUserByPage(HttpServletRequest request, Model model) { // TODO Auto-generated method stub String pageNow = request.getParameter("pageNow"); Page page = null; List<User> user = new ArrayList<User>(); //查詢用戶總數(shù) int totalCount = (int) userMapper.getUserCount(); if (pageNow != null) { page = new Page(totalCount, Integer.parseInt(pageNow)); user = this.userMapper.selectUserByPage(page.getStartPos(), page.getPageSize()); } else { page = new Page(totalCount, 1); user = this.userMapper.selectUserByPage(page.getStartPos(), page.getPageSize()); } model.addAttribute("user", user); model.addAttribute("page", page); }
5.controller方法
@Controllerpublic class UserController { @Autowired private UserService userService; @RequestMapping("/pagingUser.action") public String pagingUser(HttpServletRequest request,Model model){ userService.showUserByPage(request, model); return "pagingUser"; } }
6.頁面顯示
<c:forEach items="${user }" var="user"> <tr> <td>${user.emplnumb }</td> <td>${user.username }</td> <td>${user.address }</td> <td>${user.telephone }</td> </tr> </c:forEach></table> <div align="center"> <font size="2">共 ${page.totalPageCount} 頁</font> <font size="2">第 ${page.pageNow} 頁</font> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=1">首頁</a> <c:choose> <c:when test="${page.pageNow - 1 > 0}"> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow - 1}">上一頁</a> </c:when> <c:when test="${page.pageNow - 1 <= 0}"> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=1">上一頁</a> </c:when> </c:choose> <c:choose> <c:when test="${page.totalPageCount==0}"> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow}">下一頁</a> </c:when> <c:when test="${page.pageNow + 1 < page.totalPageCount}"> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow + 1}">下一頁</a> </c:when> <c:when test="${page.pageNow + 1 >= page.totalPageCount}"> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.totalPageCount}">下一頁</a> </c:when> </c:choose> <c:choose> <c:when test="${page.totalPageCount==0}"> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow}">尾頁</a> </c:when> <c:otherwise> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.totalPageCount}">尾頁</a> </c:otherwise> </c:choose> </div>
關(guān)于“SSM框架分頁如何實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。