很长一段时间里,我学习编程很少总结代码。后来代码总结也只是写在一个电脑里的文件夹,觉得与互联网脱轨了,哈哈哈,所以现在也准备写一写博客,记录自己,提高水平。
这是我的第一篇,也是关于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
{spring.version}
34
35
36 org.springframework
37 spring-beans
38
{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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 <c:forEach items=" {space.floor}
66
68
78
79 </c:forEach>
80
81
楼层 | 区域 | 车位号 | 状态 | 操作 |
---|---|---|---|---|
{space.spaceId} | 69 <c:if test=" {space.status == 1}">占用</c:if> 71 <c:if test=" {space.id}">编辑 76 删除 77 |
82
当前
{pageInfo.pages }
83 页,总
{pageInfo.getFirstPage()}">首页
86 <c:if test="
{pageInfo.pageNum-1}">上一页
88 </c:if>
89
90 <c:if test="
{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=" {space.floor}
24
26
36
37 </c:forEach>
38
39
楼层 | 区域 | 车位号 | 状态 | 操作 |
---|---|---|---|---|
{space.spaceId} | 27 <c:if test=" {space.status == 1}">占用</c:if> 29 <c:if test=" {space.id}">编辑 34 删除 35 |
40
当前
{pageInfo.pages }
41 页,总
{pageInfo.getFirstPage()}">首页
44 <c:if test="
{pageInfo.pageNum-1}">上一页
46 </c:if>
47
48 <c:if test="
{pageInfo.pageNum+1}">下一页
50 </c:if>
51
52 末页
53
深圳网站建设 https://www.sz886.com