关于在SSM框架下使用PageHelper

很长一段时间里,我学习编程很少总结代码。后来代码总结也只是写在一个电脑里的文件夹,觉得与互联网脱轨了,哈哈哈,所以现在也准备写一写博客,记录自己,提高水平。

这是我的第一篇,也是关于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
6 parking
7 com.dong
8 1.0-SNAPSHOT
9
10 4.0.0
11
12 parking_dao
13
14
15 mysql
16 mysql-connector-java
17 5.1.47
18
19
20 org.mybatis
21 mybatis
22 3.4.6
23
24
25
26 org.springframework
27 spring-core
28 s p r i n g . v e r s i o n &lt; / v e r s i o n &gt; 29 &lt; / d e p e n d e n c y &gt; 30 &lt; d e p e n d e n c y &gt; 31 &lt; g r o u p I d &gt; o r g . s p r i n g f r a m e w o r k &lt; / g r o u p I d &gt; 32 &lt; a r t i f a c t I d &gt; s p r i n g c o n t e x t &lt; / a r t i f a c t I d &gt; 33 &lt; v e r s i o n &gt; {spring.version}&lt;/version&gt; 29 &lt;/dependency&gt; 30 &lt;dependency&gt; 31 &lt;groupId&gt;org.springframework&lt;/groupId&gt; 32 &lt;artifactId&gt;spring-context&lt;/artifactId&gt; 33 &lt;version&gt; {spring.version}
34
35
36 org.springframework
37 spring-beans
38 s p r i n g . v e r s i o n &lt; / v e r s i o n &gt; 39 &lt; / d e p e n d e n c y &gt; 40 &lt; d e p e n d e n c y &gt; 41 &lt; g r o u p I d &gt; o r g . m y b a t i s &lt; / g r o u p I d &gt; 42 &lt; a r t i f a c t I d &gt; m y b a t i s s p r i n g &lt; / a r t i f a c t I d &gt; 43 &lt; v e r s i o n &gt; 1.3.1 &lt; / v e r s i o n &gt; 44 &lt; / d e p e n d e n c y &gt; 45 &lt; d e p e n d e n c y &gt; 46 &lt; g r o u p I d &gt; o r g . s p r i n g f r a m e w o r k &lt; / g r o u p I d &gt; 47 &lt; a r t i f a c t I d &gt; s p r i n g j d b c &lt; / a r t i f a c t I d &gt; 48 &lt; v e r s i o n &gt; {spring.version}&lt;/version&gt; 39 &lt;/dependency&gt; 40 &lt;dependency&gt; 41 &lt;groupId&gt;org.mybatis&lt;/groupId&gt; 42 &lt;artifactId&gt;mybatis-spring&lt;/artifactId&gt; 43 &lt;version&gt;1.3.1&lt;/version&gt; 44 &lt;/dependency&gt; 45 &lt;dependency&gt; 46 &lt;groupId&gt;org.springframework&lt;/groupId&gt; 47 &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt; 48 &lt;version&gt; {spring.version}
49
50
51 com.github.pagehelper
52 pagehelper
53 5.1.2
54
55
56
57
复制代码

最主要的是:

com.github.pagehelper pagehelper 5.1.2 随后需要在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
12
13 <property name=“url”
14 value=“jdbc:mysql://localhost:3306/car_park?useUnicode=true&characterEncoding=utf-8&useSSL=false”/>
15
16
17
18
19
20
21
22
23
24
25
26
27
28 offsetAsPageNum=true
29 rowBoundsWithCount=true
30 pageSizeZero=true
31 reasonable=true
32
33
34
35
36
37
38
39
40
41
42
复制代码
最主要的为:

复制代码
1
2
3
4
5
6
7
8
9
10 offsetAsPageNum=true
11 rowBoundsWithCount=true
12 pageSizeZero=true
13 reasonable=true
14
15
16
17
18
19
复制代码
Ok,关于配置已经好了,现在进入parking,结构如下,以红框标识的为例

ParkSpaceController代码如下

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 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 pageInfo = new PageInfo(list);
14 map.put(“pageInfo”, pageInfo);
15 return “park_space_list”;
16 }
复制代码
这里的形参pageNo为前台传来的值。这里的步骤可归结如下

步骤1:PageHelper.startPage(pageNo,pageSize),设置每页显示的个数和页数

步骤2:List list = beanService.find(),获得bean的结合(这里以Bean代表一般的实体类)

步骤3:PageInfo pageInfo = new PageInfo(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


7

8

9

员工列表


10


11

12

13

14

15

16
30

31

32
33
34
35
36
37
38
39

40

41

42

43

44
45
46
47
48
49
50
51
52
53
54
55
56
57 <c:forEach items=" p a g e I n f o . l i s t &quot; v a r = &quot; s p a c e &quot; &gt; 58 &lt; t r c l a s s = &quot; m e s s a g e u n r e a d &quot; &gt; 59 &lt; t d c l a s s = &quot; h i d d e n x s &quot; &gt; 60 &lt; l a b e l c l a s s = &quot; o p t i o n b l o c k m n &quot; &gt; 61 &lt; i n p u t t y p e = &quot; c h e c k b o x &quot; n a m e = &quot; m o b i l e o s &quot; v a l u e = &quot; F R &quot; &gt; 62 &lt; s p a n c l a s s = &quot; c h e c k b o x m n &quot; &gt; &lt; / s p a n &gt; 63 &lt; / l a b e l &gt; 64 &lt; / t d &gt; 65 &lt; t d &gt; {pageInfo.list}&quot; var=&quot;space&quot;&gt; 58 &lt;tr class=&quot;message-unread&quot;&gt; 59 &lt;td class=&quot;hidden-xs&quot;&gt; 60 &lt;label class=&quot;option block mn&quot;&gt; 61 &lt;input type=&quot;checkbox&quot; name=&quot;mobileos&quot; value=&quot;FR&quot;&gt; 62 &lt;span class=&quot;checkbox mn&quot;&gt;&lt;/span&gt; 63 &lt;/label&gt; 64 &lt;/td&gt; 65 &lt;td&gt; {space.floor}
66
68
78
79 </c:forEach>
80
81
操作
s p a c e . a r e a &lt; / t d &gt; 67 &lt; t d &gt; {space.area}&lt;/td&gt; 67 &lt;td&gt; {space.spaceId}
69 <c:if test=" s p a c e . s t a t u s = = 0 &quot; &gt; &lt; / c : i f &gt; 70 &lt; c : i f t e s t = &quot; {space.status == 0}&quot;&gt;空闲&lt;/c:if&gt; 70 &lt;c:if test=&quot; {space.status == 1}">占用</c:if>
71 <c:if test=" s p a c e . s t a t u s = = 2 &quot; &gt; &lt; / c : i f &gt; 72 &lt; / t d &gt; 7374 &lt; t d &gt; 75 &lt; a h r e f = &quot; / p a r k S p a c e / t o u p d a t e ? i d = {space.status == 2}&quot;&gt;预定&lt;/c:if&gt; 72 &lt;/td&gt; 73 74 &lt;td&gt; 75 &lt;a href=&quot;/parkSpace/to_update?id= {space.id}">编辑
76 删除
77

82

当前 p a g e I n f o . p a g e N u m , {pageInfo.pageNum }页,总 {pageInfo.pages }
83 页,总 p a g e I n f o . t o t a l 84 &lt; / p &gt; 85 &lt; a h r e f = &quot; l i s t ? p a g e N o = {pageInfo.total } 条记录 84 &lt;/p&gt; 85 &lt;a href=&quot;list?pageNo= {pageInfo.getFirstPage()}">首页
86 <c:if test=" p a g e I n f o . h a s P r e v i o u s P a g e &quot; &gt; 87 &lt; a h r e f = &quot; l i s t ? p a g e N o = {pageInfo.hasPreviousPage }&quot;&gt; 87 &lt;a href=&quot;list?pageNo= {pageInfo.pageNum-1}">上一页
88 </c:if>
89
90 <c:if test=" p a g e I n f o . h a s N e x t P a g e &quot; &gt; 91 &lt; a h r e f = &quot; l i s t ? p a g e N o = {pageInfo.hasNextPage }&quot;&gt; 91 &lt;a href=&quot;list?pageNo= {pageInfo.pageNum+1}">下一页
92 </c:if>
93
94 末页
95


96

97

98

99

100
101 <jsp:include page=“bottom.jsp”/>
复制代码
我们关注于

复制代码
1


2
3
4
5
6
7
8
9
10
11
12
13
14
15 <c:forEach items=" p a g e I n f o . l i s t &quot; v a r = &quot; s p a c e &quot; &gt; 16 &lt; t r c l a s s = &quot; m e s s a g e u n r e a d &quot; &gt; 17 &lt; t d c l a s s = &quot; h i d d e n x s &quot; &gt; 18 &lt; l a b e l c l a s s = &quot; o p t i o n b l o c k m n &quot; &gt; 19 &lt; i n p u t t y p e = &quot; c h e c k b o x &quot; n a m e = &quot; m o b i l e o s &quot; v a l u e = &quot; F R &quot; &gt; 20 &lt; s p a n c l a s s = &quot; c h e c k b o x m n &quot; &gt; &lt; / s p a n &gt; 21 &lt; / l a b e l &gt; 22 &lt; / t d &gt; 23 &lt; t d &gt; {pageInfo.list}&quot; var=&quot;space&quot;&gt; 16 &lt;tr class=&quot;message-unread&quot;&gt; 17 &lt;td class=&quot;hidden-xs&quot;&gt; 18 &lt;label class=&quot;option block mn&quot;&gt; 19 &lt;input type=&quot;checkbox&quot; name=&quot;mobileos&quot; value=&quot;FR&quot;&gt; 20 &lt;span class=&quot;checkbox mn&quot;&gt;&lt;/span&gt; 21 &lt;/label&gt; 22 &lt;/td&gt; 23 &lt;td&gt; {space.floor}
24
26
36
37 </c:forEach>
38
39
操作
s p a c e . a r e a &lt; / t d &gt; 25 &lt; t d &gt; {space.area}&lt;/td&gt; 25 &lt;td&gt; {space.spaceId}
27 <c:if test=" s p a c e . s t a t u s = = 0 &quot; &gt; &lt; / c : i f &gt; 28 &lt; c : i f t e s t = &quot; {space.status == 0}&quot;&gt;空闲&lt;/c:if&gt; 28 &lt;c:if test=&quot; {space.status == 1}">占用</c:if>
29 <c:if test=" s p a c e . s t a t u s = = 2 &quot; &gt; &lt; / c : i f &gt; 30 &lt; / t d &gt; 3132 &lt; t d &gt; 33 &lt; a h r e f = &quot; / p a r k S p a c e / t o u p d a t e ? i d = {space.status == 2}&quot;&gt;预定&lt;/c:if&gt; 30 &lt;/td&gt; 31 32 &lt;td&gt; 33 &lt;a href=&quot;/parkSpace/to_update?id= {space.id}">编辑
34 删除
35

40

当前 p a g e I n f o . p a g e N u m , {pageInfo.pageNum }页,总 {pageInfo.pages }
41 页,总 p a g e I n f o . t o t a l 42 &lt; / p &gt; 43 &lt; a h r e f = &quot; l i s t ? p a g e N o = {pageInfo.total } 条记录 42 &lt;/p&gt; 43 &lt;a href=&quot;list?pageNo= {pageInfo.getFirstPage()}">首页
44 <c:if test=" p a g e I n f o . h a s P r e v i o u s P a g e &quot; &gt; 45 &lt; a h r e f = &quot; l i s t ? p a g e N o = {pageInfo.hasPreviousPage }&quot;&gt; 45 &lt;a href=&quot;list?pageNo= {pageInfo.pageNum-1}">上一页
46 </c:if>
47
48 <c:if test=" p a g e I n f o . h a s N e x t P a g e &quot; &gt; 49 &lt; a h r e f = &quot; l i s t ? p a g e N o = {pageInfo.hasNextPage }&quot;&gt; 49 &lt;a href=&quot;list?pageNo= {pageInfo.pageNum+1}">下一页
50 </c:if>
51
52 末页
53


深圳网站建设 https://www.sz886.com

猜你喜欢

转载自blog.csdn.net/chenmh12/article/details/91431432