1. 程式人生 > >SSM框架下分頁的實現(封裝page.java和List<?>)

SSM框架下分頁的實現(封裝page.java和List<?>)

添加 interface jsp頁面 har show 初始化 ring array dex

之前寫過一篇博客 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<Student> 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     <!-- 分頁SQL語句 -->  
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<Student> stuList=new ArrayList<Student>();
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<Student> stuList = (List<Student>) 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 here</title>
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}"><!-- 如果總頁數小於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 }"><!-- 如果總頁數小於5,則顯示1-10頁 -->
25                 <c:set var="begin" value="1"></c:set>
26                 <c:set var="end" value="10"></c:set>
27             </c:when>
28             <c:otherwise><!-- 否則,顯示前5頁和後5頁,保證當前頁在中間 -->
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 }"><!-- 如果end值小於總的記錄數,則顯示最後10頁 -->
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 }"><!-- 如果當前頁為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

SSM框架下分頁的實現(封裝page.java和List<?>)