SSM框架下分页的实现(封装page.java和List

时间:2025-01-16 20:04:20

之前写过一篇博客  java分页的实现(后台工具类和前台jsp页面),介绍了分页的原理。

今天整合了Spring和SpringMVC和MyBatis,做了增删改查和分页,之前的逻辑都写在了Servlet里,

如今用了SSM框架,业务逻辑应该放在业务层(service),

这里有一个小问题:实现分页时,我们需要向页面中传两个参数:

  • page对象(封装了页码,页容,总页数,总记录数,取得选择记录的初始位置)
  • 集合对象(封装了bean类的信息)

也就是说,我们需要从service层获取到两个值,但是一个函数只有返回值(?),如何解决?

(1)第一种:写两个函数不就行了;

(2)第二种:添加引用;

(3)第三种:封装:

  ① 将 List 封装到 Page 中

  ② 新建一个类,封装 Page 和 List

个人认为,分开写比较好(3.2),

1、新建一个 LimitPageList

 public class LimitPageList {
private Page page;
private List<?> list;
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}

Page工具类

 public class Page implements Serializable {
private static final long serialVersionUID = -3198048449643774660L; private int pageNow = 1; // 当前页数 private int pageSize = 10; // 每页显示记录的条数 private int totalCount; // 总的记录条数 private int totalPageCount; // 总的页数 @SuppressWarnings("unused")
private int startPos; // 开始位置,从0开始 public Page(){} //通过构造函数 传入 总记录数 和 当前页
public Page(int totalCount, int pageNow) {
this.totalCount = totalCount;
this.pageNow = pageNow;
} //取得总页数,总页数=总记录数/每页显示记录的条数
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;
} //取得选择记录的初始位置
public int getStartPos() {
return (pageNow - 1) * pageSize;
} }

Page

2、在mapper层实现获取分页记录获取总的记录数的方法,并在 xml 文件中做实现

 public interface StudentMapper {
/**
* 获取分页记录
* @param startPos:从数据库中第几行开始获取
* @param pageSize:获取的条数
* @return 返回pageSize条数据的集合,数据足够多
*/
List<Student> selectByPage(@Param(value = "startPos") Integer startPos,
@Param(value = "pageSize") Integer pageSize); /**
* 获取数据库总的记录数
* @return 返回数据库表的总条数
*/
int getCount(); }

StudentMapper.java

 <?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="com.bwlu.mapper.StudentMapper" >
<resultMap id="BaseResultMap" type="com.bwlu.bean.Student" >
<id column="stu_id" property="stu_id" jdbcType="INTEGER" />
<result column="stu_name" property="stu_name" jdbcType="VARCHAR" />
<result column="stu_age" property="stu_age" jdbcType="INTEGER" />
<result column="stu_gender" property="stu_gender" jdbcType="INTEGER" />
</resultMap> <!-- 分页SQL语句 -->
<select id="selectByPage" resultMap="BaseResultMap">
select * from student limit #{startPos},#{pageSize}
</select> <!-- 取得记录的总数 -->
<select id="getCount" resultType="java.lang.Integer">
SELECT COUNT(*) FROM student
</select> </mapper>

StudentMapper.xml

3、在service层添加业务逻辑(方法)

 @Autowired
private StudentMapper studentMapper;
/**
* 获取分页记录
* @param pageNow:当前页码,若为null值,则为1
* @return 返回page和list集合
*/
public LimitPageList getLimitPageList(Integer pageNow) {
LimitPageList LimitPageStuList = new LimitPageList();
int totalCount=studentMapper.getCount();//获取总的记录数
List<Student> stuList=new ArrayList<Student>();
Page page=null;
if(pageNow!=null){
page=new Page(totalCount, pageNow);
page.setPageSize(4);
stuList=studentMapper.selectByPage(page.getStartPos(), page.getPageSize());//从startPos开始,获取pageSize条数据
}else{
page=new Page(totalCount, 1);//初始化pageNow为1
page.setPageSize(4);
stuList=studentMapper.selectByPage(page.getStartPos(), page.getPageSize());//从startPos开始,获取pageSize条数据
}
LimitPageStuList.setPage(page);
LimitPageStuList.setList(stuList);
return LimitPageStuList;
}

4、controller实现

 @Autowired
private StudentService studentService;
@RequestMapping(value="/show",method=RequestMethod.GET)
public String getStuList(Model m,@RequestParam(value="pageNow",required=false) Integer pageNow){
LimitPageList limitPageStuList = studentService.getLimitPageList(pageNow);
Page page = limitPageStuList.getPage();
//强制类型转换
List<Student> stuList = (List<Student>) limitPageStuList.getList();
m.addAttribute("page", page);
m.addAttribute("stuList", stuList);
return "student/showInfo";
}

5、页面实现

 <%@page import="com.bwlu.common.Page"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String rootPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<!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>Insert title here</title>
<link href="<%=rootPath %>public/css/pageBar.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="<%=rootPath %>public/js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="<%=rootPath %>public/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<div class="page_nav">
<c:choose>
<c:when test="${page.totalPageCount <= 10}"><!-- 如果总页数小于10,则全部显示 -->
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="${page.totalPageCount }"></c:set>
</c:when>
<c:when test="${page.pageNow <= 5 }"><!-- 如果总页数小于5,则显示1-10页 -->
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="10"></c:set>
</c:when>
<c:otherwise><!-- 否则,显示前5页和后5页,保证当前页在中间 -->
<c:set var="begin" value="${page.pageNow-5 }"></c:set>
<c:set var="end" value="${page.pageNow+5 }"></c:set>
<c:if test="${end > page.totalPageCount }"><!-- 如果end值小于总的记录数,则显示最后10页 -->
<c:set var="end" value="${page.totalPageCount}"></c:set>
<c:set var="begin" value="${end-10 }"></c:set>
</c:if>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.pageNow != 1 }"><!-- 如果当前页为1,则不显示首页和上一页 -->
<a href="?pageNow=1">首页</a>
<a href="?pageNow=${page.pageNow-1 }">上一页</a>
</c:when>
</c:choose>
<!-- 遍历页码 -->
<c:forEach begin="${begin }" end="${end }" var="index">
<c:choose>
<c:when test="${page.pageNow == index }"><!-- 如果为当前页,则特殊显示 -->
<a style="height:24px; margin:0 3px; border:none; background:#C00;">${index}</a>
</c:when>
<c:otherwise><!-- 否则,普通显示 -->
<a href="?pageNow=${index }">${index }</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${page.pageNow != page.totalPageCount }"><!-- 如果当前页为总的记录数,则不显示末页和下一页 -->
<a href="?pageNow=${page.pageNow+1 }">下一页</a>
<a href="?pageNow=${page.totalPageCount }">末页</a>
</c:when>
</c:choose>
共${page.totalPageCount }页,${page.totalCount }条记录 到第<input
value="${page.pageNow }" name="pn" id="pn_input" />页 <input
id="pn_btn" type="button" value="确定">
<script type="text/javascript">
//为按钮绑定一个单击响应函数
$("#pn_btn").click(function() {
//获取到要跳转的页码
var pageNow = $("#pn_input").val();
//通过修改window.location属性跳转到另一个页面
window.location = "?pageNow=" + pageNow;
});
</script>
</div>
</body>
</html>

page.jsp