[TOC]
# 分页
## 步骤 1 : 先运行,看到效果,再学习
先将完整的项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
## 步骤 2 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。
## 步骤 3 : 基于前面的知识点
本知识点基于SSM整合进行
## 步骤 4 : 本知识点效果
访问页面看到如图所示效果
`http://127.0.0.1:8080/ssm/listCategory`
![](https://box.kancloud.cn/c1291ffeba648ffa5cf55216204aac2b_620x316.png)
## 步骤 5 : 分页类:Page
Page类用于存放分页信息:
start: 开始位置
count: 每页的个数
last: 最后一页的位置
caculateLast()方法: 通过总数total和每页的个数计算出最后一页的位置
~~~
package com.dodoke.util;
public class Page {
private int start = 0;
private int count = 5;
private int last = 0;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getLast() {
return last;
}
public void setLast(int last) {
this.last = last;
}
public void calculateLast(int total) {
// 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
if (0 == total % count) {
last = total - count;
}
// 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
else {
last = total - total % count;
}
}
}
~~~
## 步骤 6 : Category.xml
修改list,根据当有分页信息的时候,进行分页查询
增加total sql语句
~~~
<?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
<if test="start!=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="total" resultType="int">
select count(*) from category
</select>
</mapper>
~~~
## 步骤 7 : CategoryMapper
增加total方法用于调用Category.xml 中total对应的sql语句
增加 list(Page page),根据分页来查询数据
~~~
package com.dodoke.mapper;
import java.util.List;
import com.dodoke.pojo.Category;
import com.dodoke.util.Page;
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();
public List<Category> list(Page page);
public int total();
}
~~~
## 步骤 8 : CategoryService
增加total用于获取所有
增加 list(Page page),根据分页来查询数据
~~~
package com.dodoke.service;
import java.util.List;
import com.dodoke.pojo.Category;
import com.dodoke.util.Page;
public interface CategoryService {
List<Category> list();
int total();
List<Category> list(Page page);
}
~~~
## 步骤 9 : CategoryServiceImpl
实现total()和list(Page 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;
import com.dodoke.util.Page;
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryMapper categoryMapper;
@Override
public List<Category> list() {
// TODO Auto-generated method stub
return categoryMapper.list();
}
@Override
public int total() {
return categoryMapper.total();
}
@Override
public List<Category> list(Page page) {
return categoryMapper.list(page);
}
}
~~~
## 步骤 10 : CategoryController
1. 修改listCategory,接受分页信息的注入
` listCategory(Page page)`
2. 根据分页对象,进行查询获取对象集合cs
`List<Category> cs= categoryService.list(page);`
3. 根据总数,计算出最后一页的信息
`int total = categoryService.total();`
~~~
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;
//告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory(Page page) {
int total = categoryService.total();
page.calculateLast(total);
// 边界条件判断
// 首页,点击上一页,页面没有负数
int start = page.getStart();
if (start < 0) {
page.setStart(0);
}
// 末页,点击下一页,开始页面不能超过最后一页
if (start > page.getLast()) {
page.setStart(page.getLast());
}
ModelAndView mav = new ModelAndView();
List<Category> cs = categoryService.list(page);
// 放入转发参数
mav.addObject("cs",cs);
// 放入jsp路径
mav.setViewName("listCategory");
return mav;
}
}
~~~
## 步骤 11 : listCategory.jsp
修改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>
<a href="?start=${page.start - page.count }">上一页</a>
<a href="?start=${page.start + page.count }">下一页</a>
<a href="?start=${page.last }">末 页</a>
</div>
</body>
</html>
~~~
## 步骤 12 : 增加100个对象,用于测试
修改MybatisTest 类,新增100个对象,用于分页测试
~~~
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 : 测试
访问页面看到如图所示效果
`http://127.0.0.1:8080/ssm/listCategory`
![](https://box.kancloud.cn/c1291ffeba648ffa5cf55216204aac2b_620x316.png)
## 常见问题
1. 为什么在Controller类里没有把page放入mav里,jsp里也能用到呢?
> 这句mav.addObject("page", page);框架已经省略了,可写可不写的,作为参数会被自动放进去的.
2. 我看到URL在点击链接之后变了,这里是提交了start么,提交了之后又是怎么把它赋给page对象的呢(像表单提交一样)
~~~
<a href="?start=${page.start-page.count}">上一页</a>
~~~
>是的,就是因为提交了start参数。 springmvc框架会自动把start参数注入到Page对象的start属性上。
## 补充说明
> 分页界限判断,还可以使用JSTL中的if标签进行判断,提供个思路
>
~~~
<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>
~~~
- 前言
- 计算机概论
- 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
- 缺勤记录
- 班级备忘录
- 违纪统计