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

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

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

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

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

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

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

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

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

(3)第三种:封装:

  ① 将 List 封装到 Page 中

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

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

1、新建一个 LimitPageList

 1 public class LimitPageList {
 2     private Page page;
 3     private List list;
 4     public Page getPage() {
 5         return page;
 6     }
 7     public void setPage(Page page) {
 8         this.page = page;
 9     }
10     public List getList() {
11         return list;
12     }
13     public void setList(List list) {
14         this.list = list;
15     }
16 }

Page工具类

 1 public class Page implements Serializable {
 2     private static final long serialVersionUID = -3198048449643774660L;
 3 
 4     private int pageNow = 1; // 当前页数
 5 
 6     private int pageSize = 10; // 每页显示记录的条数
 7 
 8     private int totalCount; // 总的记录条数
 9 
10     private int totalPageCount; // 总的页数
11 
12     @SuppressWarnings("unused")
13     private int startPos; // 开始位置,从0开始
14     
15     public Page(){}
16     
17     //通过构造函数 传入 总记录数 和 当前页
18     public Page(int totalCount, int pageNow) {
19         this.totalCount = totalCount;
20         this.pageNow = pageNow;
21     }
22 
23     //取得总页数,总页数=总记录数/每页显示记录的条数
24     public int getTotalPageCount() {
25         totalPageCount = getTotalCount() / getPageSize();
26         return (totalCount % pageSize == 0) ? totalPageCount : totalPageCount + 1;
27     }
28 
29     public void setTotalPageCount(int totalPageCount) {
30         this.totalPageCount = totalPageCount;
31     }
32 
33     public int getPageNow() {
34         return pageNow;
35     }
36 
37     public void setPageNow(int pageNow) {
38         this.pageNow = pageNow;
39     }
40 
41     public int getPageSize() {
42         return pageSize;
43     }
44 
45     public void setPageSize(int pageSize) {
46         this.pageSize = pageSize;
47     }
48 
49     public int getTotalCount() {
50         return totalCount;
51     }
52 
53     public void setTotalCount(int totalCount) {
54         this.totalCount = totalCount;
55     }
56 
57     //取得选择记录的初始位置
58     public int getStartPos() {
59         return (pageNow - 1) * pageSize;
60     }
61     
62 }
Page

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

 1 public interface StudentMapper {
 2     /**
 3      * 获取分页记录
 4      * @param startPos:从数据库中第几行开始获取
 5      * @param pageSize:获取的条数
 6      * @return 返回pageSize条数据的集合,数据足够多
 7      */
 8     List selectByPage(@Param(value = "startPos") Integer startPos,
 9             @Param(value = "pageSize") Integer pageSize);
10     
11     /**
12      * 获取数据库总的记录数
13      * @return 返回数据库表的总条数
14      */
15     int getCount();
16     
17 }
StudentMapper.java
 1 xml version="1.0" encoding="UTF-8" ?>
 2 DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.bwlu.mapper.StudentMapper" >
 4   <resultMap id="BaseResultMap" type="com.bwlu.bean.Student" >
 5     <id column="stu_id" property="stu_id" jdbcType="INTEGER" />
 6     <result column="stu_name" property="stu_name" jdbcType="VARCHAR" />
 7     <result column="stu_age" property="stu_age" jdbcType="INTEGER" />
 8     <result column="stu_gender" property="stu_gender" jdbcType="INTEGER" />
 9   resultMap>
10   
11       
12     <select id="selectByPage" resultMap="BaseResultMap">  
13         select *  from student limit #{startPos},#{pageSize}
14     select>  
15     
16       
17     <select id="getCount" resultType="java.lang.Integer">
18         SELECT COUNT(*) FROM student  
19     select> 
20     
21 mapper>
StudentMapper.xml

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

 1 @Autowired
 2 private StudentMapper studentMapper;
 3 /**
 4  * 获取分页记录
 5  * @param pageNow:当前页码,若为null值,则为1
 6  * @return 返回page和list集合
 7  */
 8 public LimitPageList getLimitPageList(Integer pageNow) {
 9     LimitPageList LimitPageStuList = new LimitPageList();
10     int totalCount=studentMapper.getCount();//获取总的记录数
11     List stuList=new ArrayList();
12     Page page=null;
13     if(pageNow!=null){
14         page=new Page(totalCount, pageNow);
15         page.setPageSize(4);
16         stuList=studentMapper.selectByPage(page.getStartPos(), page.getPageSize());//从startPos开始,获取pageSize条数据
17     }else{
18         page=new Page(totalCount, 1);//初始化pageNow为1
19         page.setPageSize(4);
20         stuList=studentMapper.selectByPage(page.getStartPos(), page.getPageSize());//从startPos开始,获取pageSize条数据
21     }
22     LimitPageStuList.setPage(page);
23     LimitPageStuList.setList(stuList);
24     return LimitPageStuList;
25 }

4、controller实现

 1 @Autowired
 2 private StudentService studentService;
 3 @RequestMapping(value="/show",method=RequestMethod.GET)
 4 public String getStuList(Model m,@RequestParam(value="pageNow",required=false) Integer pageNow){
 5     LimitPageList limitPageStuList = studentService.getLimitPageList(pageNow);
 6     Page page = limitPageStuList.getPage();
 7     //强制类型转换
 8     List stuList = (List) limitPageStuList.getList();
 9     m.addAttribute("page", page);
10     m.addAttribute("stuList", stuList);
11     return "student/showInfo";
12 }

5、页面实现

 1 <%@page import="com.bwlu.common.Page"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 5 <%
 6     String rootPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
 7 %>
 8 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>Insert title heretitle>
13 <link href="<%=rootPath %>public/css/pageBar.css" rel="stylesheet" type="text/css"/>
14 <script type="text/javascript" src="<%=rootPath %>public/js/jquery-3.2.1.js">script>
15 <script type="text/javascript" src="<%=rootPath %>public/js/jquery-3.2.1.min.js">script>
16 head>
17 <body>
18     <div class="page_nav">
19         <c:choose>
20             <c:when test="${page.totalPageCount <= 10}">
21                 <c:set var="begin" value="1">c:set>
22                 <c:set var="end" value="${page.totalPageCount }">c:set>
23             c:when>
24             <c:when test="${page.pageNow <= 5 }">
25                 <c:set var="begin" value="1">c:set>
26                 <c:set var="end" value="10">c:set>
27             c:when>
28             <c:otherwise>
29                 <c:set var="begin" value="${page.pageNow-5 }">c:set>
30                 <c:set var="end" value="${page.pageNow+5 }">c:set>
31                 <c:if test="${end > page.totalPageCount }">
32                     <c:set var="end" value="${page.totalPageCount}">c:set>
33                     <c:set var="begin" value="${end-10 }">c:set>
34                 c:if>
35             c:otherwise>
36         c:choose>
37         <c:choose>
38             <c:when test="${page.pageNow != 1 }">
39                 <a href="?pageNow=1">首页a> 
40                 <a href="?pageNow=${page.pageNow-1 }">上一页a>
41             c:when>
42         c:choose>
43         
44         <c:forEach begin="${begin }" end="${end }" var="index">
45             <c:choose>
46                 <c:when test="${page.pageNow == index }">
47                     <a style="height:24px; margin:0 3px; border:none; background:#C00;">${index}a>
48                 c:when>
49                 <c:otherwise>
50                     <a href="?pageNow=${index }">${index }a>
51                 c:otherwise>
52             c:choose>
53         c:forEach>
54         <c:choose>
55             <c:when test="${page.pageNow != page.totalPageCount }">
56                 <a href="?pageNow=${page.pageNow+1 }">下一页a> 
57                 <a href="?pageNow=${page.totalPageCount }">末页a>
58             c:when>
59         c:choose>
60         共${page.totalPageCount }页,${page.totalCount }条记录 到第<input
61             value="${page.pageNow }" name="pn" id="pn_input" /><input
62             id="pn_btn" type="button" value="确定">
63         <script type="text/javascript">
64             //为按钮绑定一个单击响应函数
65             $("#pn_btn").click(function() {
66                 //获取到要跳转的页码
67                 var pageNow = $("#pn_input").val();
68                 //通过修改window.location属性跳转到另一个页面
69                 window.location = "?pageNow=" + pageNow;
70             });
71         script>
72     div>
73 body>
74 html>
page.jsp

 

转载于:https://www.cnblogs.com/lixiang1993/p/7474505.html

你可能感兴趣的:(SSM框架下分页的实现(封装page.java和List))