[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`

- 前言
- 計算機概論
- MySQL數據庫
- 數據庫介紹
- MySQL的安裝
- SQL
- 表基本操作
- 修改數據語句
- 數據檢索操作
- 多表數據操作
- 表結構設計
- 綜合應用
- SQL-SERVER
- 數據庫介紹
- SQL-SERVER安裝
- SQL
- 表基本操作
- JAVA
- JAVA 介紹
- JAVA 運行原理
- JDK 配置
- 類和對象
- 數據類型
- 變量
- 直接量
- 運算符
- 流程控制
- 數組結構
- 面向對象
- 隱藏和封裝
- 深入構造器
- 類的繼承
- 多態
- 包裝類
- final 修飾符
- 抽象類
- 接口
- 集合框架
- 常用類學習
- 異常處理
- 設計模式-單例模式
- JDBC
- HTML基礎
- Web原理和HTML簡介
- Web原理
- HTML概念
- HTML標簽
- 標簽
- HTML固定基本結構
- 第一個HTML頁面
- 工具的使用
- 標題
- hr和p標簽
- 路徑概念
- 超級鏈接
- 列表
- 表格
- 表單的設計與使用
- 表單域的原理
- 文本框和密碼框
- 單選框和復選框
- 下拉列表框
- 多行文本和上傳
- 提交按鈕和重置按鈕
- 為CODING COFFEE加入在線購買頁
- HTML5
- 定位服務
- CSS基礎
- CSS的基礎使用
- CSS簡介
- CSS樣式規則和加載方式
- 內聯元素和區塊元素介紹
- 選擇器
- 偽類
- CSS顏色
- 背景圖片
- 文本
- CSS列表
- DIV+CSS布局
- 盒子模型的邊距和邊框
- Display屬性
- 浮動和清除浮動
- 用Position屬性進行定位
- 專題:居中和對齊
- CSS新特性
- CSS3邊框
- 動畫
- JavaScript基礎
- Hello World!
- 語句和變量
- 一切皆對象
- 標識符、注釋和區塊
- 基本數據類型和引用數據類型
- 語句
- 條件語句
- 循環語句
- 數據類型
- typeof
- number
- 字符串
- 布爾類型
- 函數
- 數組
- 運算符
- 加法運算符
- 算術、賦值、比較運算符
- 布爾運算符
- DOM模型
- DOM和DOM節點
- 特征相關屬性
- 節點對象的方法
- Element對象
- Attribute對象
- Text節點和CSS操作
- 事件模型
- 標準庫
- Number對象
- String對象
- Array對象
- Date、Boolean和Math對象
- JSON對象
- 面向對象編程中的 this
- Web Storage
- 錯誤處理機制
- Error對象和try..catch語句
- javascript的原生錯誤類型
- BOM模型
- window對象
- 計時事件
- jQuery基礎
- 認識jQuery
- jQuery對象和DOM對象
- jQuery選擇器
- jQuery Dom操作
- 查找節點和創建節點
- 插入節點和刪除節點
- 復制節點和替換節點
- 包裹節點和屬性操作
- 樣式操作
- 設置和獲取HTML、文本和值
- 遍歷節點和CSS操作
- jQuery 事件和動畫
- 事件綁定與冒泡處理
- jQuery動畫
- jQuery 插件
- validate 插件
- jQuery與Ajax的應用
- Ajax簡介
- jquery中的Ajax
- Flex布局
- Flexbox介紹
- 伸縮容器屬性介紹
- dispaly屬性
- flex-direction屬性
- flex-wrap屬性
- flex-flow屬性
- align-content屬性
- justify-content屬性
- align-items屬性
- 伸縮項目屬性介紹
- order屬性
- grow屬性
- basis屬性
- shrink屬性
- flex屬性簡寫
- align-self屬性
- Bootstrap基礎
- 起步
- 柵格系統
- 排版樣式
- 表格和按鈕
- 表單和圖片
- 輔助類和響應式工具
- 圖標菜單按鈕組件
- 輸入框和導航組件
- 路徑、分頁、標簽和徽章組件
- 巨幕、頁頭、縮略圖和警告框
- 進度、條媒體對象和Well組件
- 列表組和嵌入組件
- Canvas
- Canvas坐標體系
- Canvas畫布大小設置
- Canvas畫直線
- Canvas畫圓和矩形
- Canvas描邊與填充
- Canvas圖形變換
- Canvas線性漸變
- Canvas徑向漸變
- Canvas中的文字
- Canvas圖片繪制
- Canvas圖形畫刷
- Canvas剪輯區域
- Canvas繪制陰影
- Canvas繪制曲線
- Canvas動畫
- Canvas離屏操作
- 微信小程序
- 起步
- 小程序目錄
- 小程序配置
- 新建頁面
- WXML
- 組件
- 視圖容器
- 基礎內容
- 表單組件
- button
- checkbox
- form
- input
- label
- picker
- picker-view
- radio
- slider
- switch
- textarea
- 導航
- 媒體組件
- audio
- image
- video
- camera
- live-player
- live-pusher
- 地圖
- 畫布
- 數據綁定
- 運算
- 組合
- 列表渲染
- 條件渲染
- 模板
- 事件
- WXSS
- JS
- JSP&Servlet
- Web應用
- Tomcat
- JSP
- Scriptlet
- Page 指令
- 包含指令
- 跳轉指令
- 用戶注冊實例
- JSP練習
- 內置對象
- Servlet
- 過濾器
- Web分層思想
- EL表達式
- JSTL
- 分頁實現
- AJAX&JSON
- 開發步驟
- 路徑問題
- Log4j
- 電子書城
- 案例分析
- 核心代碼
- Java高級
- 文件操作
- 泛型
- 類加載機制和反射
- 注解 Annotation
- Mybatis框架
- 框架介紹
- Mybatis簡單實現
- 表基本操作
- 優化配置文件
- 表字段名與實體類屬性名不同的解決方案
- 一對一關聯
- 一對多關聯
- Spring框架
- IOC/DI
- 注入對象
- 注解方式 IOC/DI
- AOP
- 注解方式AOP
- 注解方式測試
- Spring MVC框架
- Hello SpringMVC
- 視圖定位
- 注解方式
- 接受表單數據
- 客戶端跳轉
- Session
- 中文問題
- 上傳文件
- SSM整合
- 整合步驟
- 分頁
- PageHelper
- 連接池
- CRUD
- 事務管理
- JSON
- Maven
- 介紹
- 下載與配置MAVEN
- MAVEN倉庫
- ECLIPSE中的MAVEN設置
- ECLIPSE下創建MAVEN風格的JAVA項目
- 添加JAR包
- 創建MAVEN風格的JAVA WEB項目
- 創建SSM項目
- 使用ECLIPSE導入一個MAVEN風格的SSM項目
- 教學管理模版
- 學員名錄
- 周測統計
- 2017-10-27
- 課堂作業
- 班會紀要
- 2017-10-24
- 缺勤記錄
- 班級備忘錄
- 違紀統計