[TOC]
# PageHelper
## 步骤 1 : 基于前面的知识点
本知识点基于SSM 分页进行,SSM 分页进行是采用手动SQL方式进行,本知识点将采用PageHelper插件进行。
PageHelper是一款犀利的Mybatis分页插件,使用了这个插件之后,分页开发起来更加简单容易。
## 步骤 2 : 先运行,看到效果,再学习
先将完整的项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
## 步骤 3 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。
## 步骤 4 : 效果
访问页面看到如图所示效果
## 步骤 5 : jar包
因为是第三方插件,所以需要额外的jar包(向老师要相关资料):pagehelper-5.1.0-beta2.jar,jsqlparser-1.0.jar
,将包放在WEB-INF/lib下
## 步骤 6 : 修改applicationContext.xml
增加PageHelper插件配置
~~~
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Spring配置文件 -->
<!-- 支持注解 -->
<context:annotation-config />
<!-- 自动扫描包,将Service的生命周期纳入Spring的管理 -->
<context:component-scan base-package="com.dodoke.service" />
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/ssm_integration?characterEncoding=UTF-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<!-- MyBatis的配置 -->
<!-- 扫描存放SQL语句的xml映射文件 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.dodoke.pojo" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/dodoke/mapper/*.xml" />
<!-- PageHelper插件配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 扫描Mapper,并将其生命周期纳入Spring的管理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dodoke.mapper" />
</bean>
</beans>
~~~
## 步骤 7 : CategoryService
CategoryService去掉total方法和list(Page) 方法
~~~
package com.dodoke.service;
import java.util.List;
import com.dodoke.pojo.Category;
public interface CategoryService {
List<Category> list();
}
~~~
## 步骤 8 : CategoryServiceImpl
CategoryServiceImpl去掉total方法和list(Page) 方法
~~~
package com.dodoke.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dodoke.mapper.CategoryMapper;
import com.dodoke.pojo.Category;
import com.dodoke.service.CategoryService;
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryMapper categoryMapper;
@Override
public List<Category> list() {
return categoryMapper.list();
}
}
~~~
## 步骤 9 : CategoryMapper
CategoryMapper去掉total方法和list(Page) 方法
~~~
package com.dodoke.mapper;
import java.util.List;
import com.dodoke.pojo.Category;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public int update(Category category);
public Category get(int id);
public List<Category> list();
}
~~~
## 步骤 10 : Category.xml
Category.xml去掉total对应的sql语句,list也去掉limit
~~~
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dodoke.mapper.CategoryMapper">
<insert id="add" parameterType="Category">
insert into category(name) values(#{name})
</insert>
<delete id="delete" parameterType="Category">
delete from category where id=#{id}
</delete>
<update id="update" parameterType="Category">
update category set name=#{name} where id=#{id}
</update>
<select id="get" parameterType="int" resultType="Category">
select * from category where id=#{id}
</select>
<select id="list" resultType="Category">
select * from category
</select>
</mapper>
~~~
## 步骤 11 : CategoryController
CategoryController在调用categoryService.list(); 之前,执行:
`PageHelper.offsetPage(page.getStart(),5);`
并通过`int total = (int) new PageInfo<>(cs).getTotal();`获取总数。
其他都不变
~~~
package com.dodoke.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.dodoke.pojo.Category;
import com.dodoke.service.CategoryService;
import com.dodoke.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory(Page page) {
ModelAndView mav = new ModelAndView();
PageHelper.offsetPage(page.getStart(), 5);
List<Category> cs = categoryService.list();
int total = (int)new PageInfo<>(cs).getTotal();
page.calculateLast(total);
// 放入转发参数
mav.addObject("cs",cs);
// 放入jsp路径
mav.setViewName("listCategory");
return mav;
}
}
~~~
## 步骤 12 : MybatisTest
MybatisTest 类注释掉用旧方式分页的代码
~~~
package com.dodoke.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.dodoke.mapper.CategoryMapper;
import com.dodoke.pojo.Category;
import com.dodoke.util.Page;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
@Autowired
private CategoryMapper categoryMapper;
@Test
public void testAdd() {
for (int i = 0; i < 100; i++) {
Category category = new Category();
category.setName("new Category");
categoryMapper.add(category);
}
}
// @Test
// public void testTotal() {
// int total = categoryMapper.total();
// System.out.println(total);
// }
//
// @Test
// public void testList() {
// Page p = new Page();
// p.setStart(2);
// p.setLast(3);
// List<Category> cs = categoryMapper.list(p);
// for (Category c : cs) {
// System.out.println(c.getName());
// }
// }
}
~~~
## 步骤 13 : listCategory.jsp
增加分页临界判断
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
<div style="">
<a href="?start=0">首 页</a>
<c:if test="${page.start-page.count>=0}">
<a href="?start=${page.start-page.count}">上一页</a>
</c:if>
<c:if test="${page.start-page.count<0}">
<a href="javascript:void(0)">上一页</a>
</c:if>
<c:if test="${page.start+page.count<=page.last}">
<a href="?start=${page.start+page.count}">下一页</a>
</c:if>
<c:if test="${page.start+page.count>page.last}">
<a href="javascript:void(0)">下一页</a>
</c:if>
<a href="?start=${page.last}">末页</a>
</div>
</body>
</html>
~~~
## 步骤 14 : 重启tomcat,测试
重启tomcat,访问地址:
`http://127.0.0.1:8080/ssm/listCategory`
![](../images/Image035.png)