關於在SSM框架下使用PageHelper


首先,如果各位在這塊配置和代碼有什么問題歡迎說出來,我也會盡自己最大的能力幫大家解答

 這些代碼我都是寫在一個小項目里的,項目的github地址為:https://github.com/Albert-Best-Dong/parking,還希望哪個大佬給我這個小課題指點指點

jdk1.8,spring 4.3.18,mybatis 3.4.6,其實直接直接看pom文件就好了。

很長一段時間里,我學習編程很少總結代碼。后來代碼總結也只是寫在一個電腦里的文件夾,覺得與互聯網脫軌了,哈哈哈,所以現在也准備寫一寫博客,記錄自己,提高水平。

這是我的第一篇,也是關於SSM框架下使用PageHelper。

這里不具體寫我做的項目課題的全部內容,主要專注於PageHelper部分

工程結構如下圖:

首先在pom.xml(parking_dao模塊下)引入PageHelper依賴

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>parking</artifactId>
 7         <groupId>com.dong</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>parking_dao</artifactId>
13     <dependencies>
14         <dependency>
15             <groupId>mysql</groupId>
16             <artifactId>mysql-connector-java</artifactId>
17             <version>5.1.47</version>
18         </dependency>
19         <dependency>
20             <groupId>org.mybatis</groupId>
21             <artifactId>mybatis</artifactId>
22             <version>3.4.6</version>
23         </dependency>
24 
25         <dependency>
26             <groupId>org.springframework</groupId>
27             <artifactId>spring-core</artifactId>
28             <version>${spring.version}</version>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework</groupId>
32             <artifactId>spring-context</artifactId>
33             <version>${spring.version}</version>
34         </dependency>
35         <dependency>
36             <groupId>org.springframework</groupId>
37             <artifactId>spring-beans</artifactId>
38             <version>${spring.version}</version>
39         </dependency>
40         <dependency>
41             <groupId>org.mybatis</groupId>
42             <artifactId>mybatis-spring</artifactId>
43             <version>1.3.1</version>
44         </dependency>
45         <dependency>
46             <groupId>org.springframework</groupId>
47             <artifactId>spring-jdbc</artifactId>
48             <version>${spring.version}</version>
49         </dependency>
50         <dependency>
51             <groupId>com.github.pagehelper</groupId>
52             <artifactId>pagehelper</artifactId>
53             <version>5.1.2</version>
54         </dependency>
55     </dependencies>
56 
57 </project>

 

最主要的是:

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
隨后需要在spring配置(spring-dao)文件里配置PageHelper
 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5     http://www.springframework.org/schema/beans/spring-beans.xsd
 6     http://www.springframework.org/schema/context
 7     http://www.springframework.org/schema/context/spring-context.xsd">
 8 
 9     <context:component-scan base-package="com.dong.parking.dao"/>
10 
11     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
12         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
13         <property name="url"
14                   value="jdbc:mysql://localhost:3306/car_park?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"/>
15         <property name="username" value="root"/>
16         <property name="password" value="root"/>
17     </bean>
18 
19     <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
20         <property name="typeAliasesPackage" value="com.dong.parking.entity"/>
21         <property name="dataSource" ref="dataSource"/>
22         <!--配置PageHelper-->
23         <property name="plugins">
24             <array>
25                 <bean class="com.github.pagehelper.PageInterceptor">
26                     <property name="properties">
27                         <value>
28                             offsetAsPageNum=true
29                             rowBoundsWithCount=true
30                             pageSizeZero=true
31                             reasonable=true
32                         </value>
33                     </property>
34                 </bean>
35             </array>
36         </property>
37     </bean>
38     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
39         <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
40         <property name="basePackage" value="com.dong.parking.dao"/>
41     </bean>
42 </beans>
View Code

最主要的為:

 1 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 2         <property name="typeAliasesPackage" value="com.dong.parking.entity"/>
 3         <property name="dataSource" ref="dataSource"/>
 4         <!--配置PageHelper-->
 5         <property name="plugins">
 6             <array>
 7                 <bean class="com.github.pagehelper.PageInterceptor">
 8                     <property name="properties">
 9                         <value>
10                             offsetAsPageNum=true
11                             rowBoundsWithCount=true
12                             pageSizeZero=true
13                             reasonable=true
14                         </value>
15                     </property>
16                 </bean>
17             </array>
18         </property>
19     </bean>

Ok,關於配置已經好了,現在進入parking,結構如下,以紅框標識的為例

ParkSpaceController代碼如下

 1 package com.dong.parking.controller;
 2 
 3 import com.dong.parking.biz.ParkSpaceBiz;
 4 import com.dong.parking.entity.ParkSpace;
 5 import com.dong.parking.entity.User;
 6 import com.dong.parking.global.Constant;
 7 import com.github.pagehelper.Page;
 8 import com.github.pagehelper.PageHelper;
 9 import com.github.pagehelper.PageInfo;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestParam;
14 
15 import javax.servlet.http.HttpSession;
16 import java.util.List;
17 import java.util.Map;
18 
19 @Controller("parkSpaceController")
20 @RequestMapping("/parkSpace")
21 public class ParkSpaceController {
22     @Autowired
23     private ParkSpaceBiz parkSpaceBiz;
24 
25     @RequestMapping(value = "/list")
26     public String list(Map<String, Object> map, @RequestParam(value = "status", required = false) Integer status,
27                        @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
28         List<ParkSpace> list = null;
29         Integer pageSize = 2;
30         PageHelper.startPage(pageNo, pageSize);
31         if (status == null)
32             list = parkSpaceBiz.findAll();
33         else
34             list = parkSpaceBiz.findByStatus(status);
35 
36 
37         PageInfo<ParkSpace> pageInfo = new PageInfo<ParkSpace>(list);
38         map.put("pageInfo", pageInfo);
39         return "park_space_list";
40     }
41 
42     @RequestMapping("/to_update")
43     public String toUpdate(Map<String, Object> map, @RequestParam int id) {
44         map.put("floor", Constant.getFloor());
45         map.put("area", Constant.getArea());
46         map.put("parkSpace", parkSpaceBiz.findById(id));
47         return "park_space_update";
48     }
49 
50     @RequestMapping("/update")
51     public String update(ParkSpace parkSpace, HttpSession session) {
52         User user = (User) session.getAttribute("user");
53         parkSpace.setUpdateBy(user.getSn());
54         parkSpaceBiz.edit(parkSpace);
55         return "redirect:list";
56     }
57 
58 
59     @RequestMapping("/to_add")
60     public String toAdd(Map<String, Object> map) {
61         map.put("floor", Constant.getFloor());
62         map.put("area", Constant.getArea());
63         map.put("parkSpace", new ParkSpace());
64         return "park_space_add";
65     }
66 
67     @RequestMapping("/add")
68     public String add(ParkSpace parkSpace, HttpSession session) {
69         User user = (User) session.getAttribute("user");
70         parkSpace.setCreateBy(user.getSn());
71         parkSpace.setUpdateBy(user.getSn());
72         parkSpaceBiz.add(parkSpace);
73         return "redirect:list";
74     }
75 
76 
77     @RequestMapping(value = "/remove", params = "id")
78     public String remove(int id) {
79         parkSpaceBiz.remove(id);
80         return "redirect:list";
81     }
82 }
View Code

我們關注一下list方法

 1    @RequestMapping(value = "/list")
 2     public String list(Map<String, Object> map, @RequestParam(value = "status", required = false) Integer status,
 3                        @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
 4         List<ParkSpace> list = null;
 5         Integer pageSize = 2;
 6         PageHelper.startPage(pageNo, pageSize);
 7         if (status == null)
 8             list = parkSpaceBiz.findAll();
 9         else
10             list = parkSpaceBiz.findByStatus(status);
11 
12 
13         PageInfo<ParkSpace> pageInfo = new PageInfo<ParkSpace>(list);
14         map.put("pageInfo", pageInfo);
15         return "park_space_list";
16     }

這里的形參pageNo為前台傳來的值。這里的步驟可歸結如下

步驟1:PageHelper.startPage(pageNo,pageSize),設置每頁顯示的個數和頁數

步驟2:List<Bean> list = beanService.find(),獲得bean的結合(這里以Bean代表一般的實體類)

步驟3:PageInfo<Bean> pageInfo = new PageInfo<Bean>(list);   將list集合封裝到pageInfo對象。

步驟4:map.put("pageInfo",pageInfo);將pageInfo對象回給前台

 

好了,現在咱們來到前台頁面,park_space_list.jsp代碼如下

  1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3 
  4 <jsp:include page="top.jsp"/>
  5 
  6 <section id="content" class="table-layout animated fadeIn">
  7     <div class="tray tray-center">
  8         <div class="content-header">
  9             <h2> 員工列表 </h2>
 10             <p class="lead"></p>
 11         </div>
 12         <div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
 13             <div class="panel  heading-border">
 14                 <div class="panel-menu">
 15                     <div class="row">
 16                         <div class="hidden-xs hidden-sm col-md-3">
 17                             <div class="btn-group">
 18                                 <button type="button" class="btn btn-default light">
 19                                     <i class="fa fa-refresh"></i>
 20                                 </button>
 21                                 <button type="button" class="btn btn-default light">
 22                                     <i class="fa fa-trash"></i>
 23                                 </button>
 24                                 <button type="button" class="btn btn-default light">
 25                                     <i class="fa fa-plus"
 26                                        onclick="javascript:window.location.href='/parkSpace/to_add';"></i>
 27                                 </button>
 28                             </div>
 29                         </div>
 30                         <div class="col-xs-12 col-md-9 text-right">
 31                             <div class="btn-group">
 32                                 <button type="button" class="btn btn-default light">
 33                                     <i class="fa fa-chevron-left"></i>
 34                                 </button>
 35 
 36                                 <button type="button" class="btn btn-default light">
 37                                     <i class="fa fa-chevron-right"></i>
 38                                 </button>
 39                             </div>
 40                         </div>
 41                     </div>
 42                 </div>
 43                 <div class="panel-body pn">
 44                     <table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
 45                         <thead>
 46                         <tr class="">
 47                             <th class="text-center hidden-xs">Select</th>
 48                             <th class="hidden-xs">樓層</th>
 49                             <th class="hidden-xs">區域</th>
 50                             <th class="hidden-xs">車位號</th>
 51                             <th class="hidden-xs">狀態</th>
 52 
 53                             <th>操作</th>
 54                         </tr>
 55                         </thead>
 56                         <tbody>
 57                         <c:forEach items="${pageInfo.list}" var="space">
 58                             <tr class="message-unread">
 59                                 <td class="hidden-xs">
 60                                     <label class="option block mn">
 61                                         <input type="checkbox" name="mobileos" value="FR">
 62                                         <span class="checkbox mn"></span>
 63                                     </label>
 64                                 </td>
 65                                 <td>${space.floor}</td>
 66                                 <td>${space.area}</td>
 67                                 <td>${space.spaceId}</td>
 68                                 <td>
 69                                     <c:if test="${space.status == 0}">空閑</c:if>
 70                                     <c:if test="${space.status == 1}">占用</c:if>
 71                                     <c:if test="${space.status == 2}">預定</c:if>
 72                                 </td>
 73 
 74                                 <td>
 75                                     <a href="/parkSpace/to_update?id=${space.id}">編輯</a>
 76                                     <a href="/parkSpace/remove?id=${space.id}">刪除</a>
 77                                 </td>
 78                             </tr>
 79                         </c:forEach>
 80                         </tbody>
 81                     </table>
 82                     <p>當前 ${pageInfo.pageNum }頁,總${pageInfo.pages }
 83                         頁,總 ${pageInfo.total } 條記錄
 84                     </p>
 85                     <a href="list?pageNo=${pageInfo.getFirstPage()}">首頁</a>
 86                     <c:if test="${pageInfo.hasPreviousPage }">
 87                         <a href="list?pageNo=${pageInfo.pageNum-1}">上一頁</a>
 88                     </c:if>
 89 
 90                     <c:if test="${pageInfo.hasNextPage }">
 91                         <a href="list?pageNo=${pageInfo.pageNum+1}">下一頁</a>
 92                     </c:if>
 93 
 94                     <a href="list?pageNo=${pageInfo.getLastPage()}">末頁</a>
 95                 </div>
 96             </div>
 97         </div>
 98     </div>
 99 </section>
100 
101 <jsp:include page="bottom.jsp"/>
View Code

我們關注於

 1       <div class="panel-body pn">
 2                     <table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
 3                         <thead>
 4                         <tr class="">
 5                             <th class="text-center hidden-xs">Select</th>
 6                             <th class="hidden-xs">樓層</th>
 7                             <th class="hidden-xs">區域</th>
 8                             <th class="hidden-xs">車位號</th>
 9                             <th class="hidden-xs">狀態</th>
10 
11                             <th>操作</th>
12                         </tr>
13                         </thead>
14                         <tbody>
15                         <c:forEach items="${pageInfo.list}" var="space">
16                             <tr class="message-unread">
17                                 <td class="hidden-xs">
18                                     <label class="option block mn">
19                                         <input type="checkbox" name="mobileos" value="FR">
20                                         <span class="checkbox mn"></span>
21                                     </label>
22                                 </td>
23                                 <td>${space.floor}</td>
24                                 <td>${space.area}</td>
25                                 <td>${space.spaceId}</td>
26                                 <td>
27                                     <c:if test="${space.status == 0}">空閑</c:if>
28                                     <c:if test="${space.status == 1}">占用</c:if>
29                                     <c:if test="${space.status == 2}">預定</c:if>
30                                 </td>
31 
32                                 <td>
33                                     <a href="/parkSpace/to_update?id=${space.id}">編輯</a>
34                                     <a href="/parkSpace/remove?id=${space.id}">刪除</a>
35                                 </td>
36                             </tr>
37                         </c:forEach>
38                         </tbody>
39                     </table>
40                     <p>當前 ${pageInfo.pageNum }頁,總${pageInfo.pages }
41                         頁,總 ${pageInfo.total } 條記錄
42                     </p>
43                     <a href="list?pageNo=${pageInfo.getFirstPage()}">首頁</a>
44                     <c:if test="${pageInfo.hasPreviousPage }">
45                         <a href="list?pageNo=${pageInfo.pageNum-1}">上一頁</a>
46                     </c:if>
47 
48                     <c:if test="${pageInfo.hasNextPage }">
49                         <a href="list?pageNo=${pageInfo.pageNum+1}">下一頁</a>
50                     </c:if>
51 
52                     <a href="list?pageNo=${pageInfo.getLastPage()}">末頁</a>
53                 </div>

最終的效果如圖所示:

水平有限,表述還是很不好


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM