这篇文章主要介绍“SSM框架分页如何实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SSM框架分页如何实现”文章能帮助大家解决问题。
1.首先创建一个分页的工具类
package cn.page.po; import java.io.Serializable; public class Page implements Serializable { private static final long serialVersionUID = -3198048449643774660L; private int pageNow = 1; // 当前页数 private int pageSize = 4; // 每页显示记录的条数 private int totalCount; // 总的记录条数 private int totalPageCount; // 总的页数 @SuppressWarnings("unused") private int startPos; // 开始位置,从0开始 /** * 通过构造函数 传入 总记录数 和 当前页 * @param totalCount * @param pageNow */ public Page(int totalCount, int pageNow) { this.totalCount = totalCount; this.pageNow = pageNow; } /** * 取得总页数,总页数=总记录数/每页显示记录的条数 * @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; } }
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> <!-- 取得记录的总数 --> <select id="getUserCount" resultType="long"> SELECT COUNT(*) FROM user </select></mapper>
3.mapper接口
/** * 使用注解方式传入多个参数 * @param page * @return startPos},#{pageSize} */public List<User> selectUserByPage(@Param(value="startPos") Integer startPos, @Param(value="pageSize") Integer pageSize); /** * 用户总数 */public long getUserCount();
4.service接口与实现类
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>(); //查询用户总数 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>
关于“SSM框架分页如何实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。