文章目录
- 一、分页显示含义
- 二、分页显示优点
- 三、分页显示实现
- 3.1 分页查询
- 3.1.1 分页查询步骤
- 3.1.2 分页查询关键点
- 3.2 分页显示
- 3.2.1 分页显示关键点
- 四、MVC分层开发简单项目展示
一、分页显示含义
每次翻页的时候只从数据库里检索出本页需要的数据。
二、分页显示优点
- 数据清晰直观
- 不受数据量限制
- 页面不再冗长
三、分页显示实现
3.1 分页查询
3.1.1 分页查询步骤
- 查询总数据量
- 确定每页显示的数据量
- 计算显示的页数
页数 = (总数据量 + 每页显示的数据量 - 1)/ 每页显示的数据量 - 编写分页查询 SQL 语句
- 实现分页查询
3.1.2 分页查询关键点
- 计算显示数据的总数量需要借助 JDBC 内容
- 计算页数时,声明一个工具类将功能独立出来,便于复用
- 编写分页查询SQL语句(limit)
select * from tableName where 条件 limit (当前页码-1) * 页面容量 , 页面容量
3.2 分页显示
3.2.1 分页显示关键点
- 确定当前页
- 设置首页、上一页、下一页、末页的页码
- 对可能出现的异常进行控制
- 首页与末页的控制
- 可添加“GO”按钮,根据输入的页码,跳转到相应页码(需判断用户输入页码的正确性后再使用表单隐藏域传递页码)
四、MVC分层开发简单项目展示
这里仅展示某个项目使用分页显示技术的相关类和JSP的代码。
CommodityController.java
package com.ishopn.controller;
import java.util.List;
import com.ishopn.dao.CommodityDao;
import com.ishopn.dao.impl.CommodityDaoImpl;
import com.ishopn.model.Commodity;
public class CommodityController {
private CommodityDao commodityDao = null;
public CommodityController() {
commodityDao = new CommodityDaoImpl();
}
public List<Commodity> getCommodityList(){
return commodityDao.getCommodityList();
}
public int istCommodity(String c_name, String c_madein, String c_type, String c_inprice, String c_outprice, String c_num) {
Commodity c = new Commodity(System.currentTimeMillis()+"", c_name, c_madein, new Integer(c_type), new Integer(c_inprice), new Integer(c_outprice), new Integer(c_num));
return commodityDao.istCommodity(c);
}
public int delCommodityById(String c_id) {
return commodityDao.delCommodityById(c_id);
}
public Commodity getCommodityById(String c_id) {
return commodityDao.getCommodityById(c_id);
}
public int updCommodity(String c_id, String c_name, String c_madein, String c_type, String c_inprice, String c_outprice, String c_num) {
Commodity c = new Commodity(c_id, c_name, c_madein, new Integer(c_type), new Integer(c_inprice), new Integer(c_outprice), new Integer(c_num));
return commodityDao.updCommodity(c);
}
}
CommodityDao.java
package com.ishopn.dao;
import java.util.List;
import com.ishopn.model.Commodity;
public interface CommodityDao {
List<Commodity> getCommodityList();
int istCommodity(Commodity c);
int delCommodityById(String c_id);
Commodity getCommodityById(String c_id);
int updCommodity(Commodity c);
public List<Commodity> getCommodityListWithPage(int pageNo, int pageSize);
public int getCommodityCount();
}
CommodityDaoImpl.java
package com.ishopn.dao.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.ishopn.dao.CommodityDao;
import com.ishopn.model.Commodity;
import com.ishopn.util.DBHelper;
public class CommodityDaoImpl implements CommodityDao{
@Test
public List<Commodity> getCommodityList() {
List<Commodity> commodities = new ArrayList<Commodity>();
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select * from Commodity";
ResultSet rs = DBHelper.executeQuery(conn, sql, null);
while(rs.next()) {
Commodity c = new Commodity();
c.setC_id(rs.getString("c_id"));
c.setC_name(rs.getString("c_name"));
c.setC_madein(rs.getString("c_madein"));
c.setC_type(rs.getInt("c_type"));
c.setC_inprice(rs.getInt("c_inprice"));
c.setC_outprice(rs.getInt("c_outprice"));
c.setC_num(rs.getInt("c_num"));
c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getInt("c_type")));
commodities.add(c);
}
DBHelper.closeConnection(conn);
}catch(Exception e) {
e.printStackTrace();
}
return commodities;
}
@Override
public int istCommodity(Commodity c) {
int line = 0;
Connection conn = DBHelper.getConnection();
String sql = "insert into commodity (c_id,c_name,c_madein,c_type,c_inprice,c_outprice,c_num) values (?,?,?,?,?,?,?)";
List param = new ArrayList();
param.add(c.getC_id());
param.add(c.getC_name());
param.add(c.getC_madein());
param.add(c.getC_type());
param.add(c.getC_inprice());
param.add(c.getC_outprice());
param.add(c.getC_num());
line = DBHelper.executeUpdate(conn, sql, param);
DBHelper.closeConnection(conn);
return line;
}
@Override
public int delCommodityById(String c_id) {
int line = 0;
Connection conn = DBHelper.getConnection();
String sql = "delete from commodity where c_id=?";
List param = new ArrayList();
param.add(c_id);
line = DBHelper.executeUpdate(conn, sql, param);
DBHelper.closeConnection(conn);
return line;
}
@Override
public Commodity getCommodityById(String c_id) {
Commodity c = new Commodity();
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select * from Commodity where c_id=?";
List param = new ArrayList();
param.add(c_id);
ResultSet rs = DBHelper.executeQuery(conn, sql, param);
rs.next();
c.setC_id(rs.getString("c_id"));
c.setC_name(rs.getString("c_name"));
c.setC_madein(rs.getString("c_madein"));
c.setC_type(rs.getInt("c_type"));
c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getInt("c_type")));
c.setC_inprice(rs.getInt("c_inprice"));
c.setC_outprice(rs.getInt("c_outprice"));
c.setC_num(rs.getInt("c_num"));
DBHelper.closeConnection(conn);
}catch(Exception e) {
}
return c;
}
@Override
public int updCommodity(Commodity c) {
int line = 0;
Connection conn = DBHelper.getConnection();
String sql = "update commodity set c_name=?,c_madein=?,c_type=?,c_inprice=?,c_outprice=?,c_num=? where c_id=?";
List param = new ArrayList();
param.add(c.getC_name());
param.add(c.getC_madein());
param.add(c.getC_type());
param.add(c.getC_inprice());
param.add(c.getC_outprice());
param.add(c.getC_num());
param.add(c.getC_id());
line = DBHelper.executeUpdate(conn, sql, param);
DBHelper.closeConnection(conn);
return line;
}
@Override
public List<Commodity> getCommodityListWithPage(int pageNo, int pageSize) {
List<Commodity> list = new ArrayList<Commodity>();
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select * from commodity limit ?,?";
List param = new ArrayList();
param.add((pageNo - 1) * pageSize);
param.add(pageSize);
ResultSet rs = DBHelper.executeQuery(conn, sql, param);
while(rs.next()) {
Commodity c = new Commodity();
c.setC_id(rs.getString("c_id"));
c.setC_name(rs.getString("c_name"));
c.setC_madein(rs.getString("c_madein"));
c.setC_type(rs.getInt("c_type"));
c.setC_inprice(rs.getInt("c_inprice"));
c.setC_outprice(rs.getInt("c_outprice"));
c.setC_num(rs.getInt("c_num"));
c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getInt("c_type")));
list.add(c);
}
DBHelper.closeConnection(conn);
}catch(Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public int getCommodityCount() {
int count = 0;
Connection conn;
try {
conn = DBHelper.getConnection();
String sql = "select count(*) from commodity";
ResultSet rs = DBHelper.executeQuery(conn, sql, null);
rs.next();
count = rs.getInt(1);
DBHelper.closeConnection(conn);
}catch(Exception e) {
e.printStackTrace();
}
return count;
}
}
Commodity.java
package com.ishopn.model;
/**
* 商品表
* @author Sowhite
*
*/
public class Commodity {
private String c_id;
private String c_name;
private String c_madein;
private Integer c_type; // c_type能够确定位移的ct 1
private Commoditytype ct; // ct(ct_id:1, ct_name:'玩具')
private Integer c_inprice;
private Integer c_outprice;
private Integer c_num;
public Commodity() {
super();
}
public Commodity(String c_id, String c_name, String c_madein, Integer c_type, Commoditytype ct, Integer c_inprice,
Integer c_outprice, Integer c_num) {
super();
this.c_id = c_id;
this.c_name = c_name;
this.c_madein = c_madein;
this.c_type = c_type;
this.ct = ct;
this.c_inprice = c_inprice;
this.c_outprice = c_outprice;
this.c_num = c_num;
}
public Commodity(String c_id, String c_name, String c_madein, Integer c_type, Integer c_inprice,
Integer c_outprice, Integer c_num) {
super();
this.c_id = c_id;
this.c_name = c_name;
this.c_madein = c_madein;
this.c_type = c_type;
this.c_inprice = c_inprice;
this.c_outprice = c_outprice;
this.c_num = c_num;
}
public String getC_id() {
return c_id;
}
public void setC_id(String c_id) {
this.c_id = c_id;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public String getC_madein() {
return c_madein;
}
public void setC_madein(String c_madein) {
this.c_madein = c_madein;
}
public Integer getC_type() {
return c_type;
}
public void setC_type(Integer c_type) {
this.c_type = c_type;
}
public Commoditytype getCt() {
return ct;
}
public void setCt(Commoditytype ct) {
this.ct = ct;
}
public Integer getC_inprice() {
return c_inprice;
}
public void setC_inprice(Integer c_inprice) {
this.c_inprice = c_inprice;
}
public Integer getC_outprice() {
return c_outprice;
}
public void setC_outprice(Integer c_outprice) {
this.c_outprice = c_outprice;
}
public Integer getC_num() {
return c_num;
}
public void setC_num(Integer c_num) {
this.c_num = c_num;
}
@Override
public String toString() {
return "Commodity [c_id=" + c_id + ", c_name=" + c_name + ", c_madein=" + c_madein + ", c_type=" + c_type
+ ", ct=" + ct + ", c_inprice=" + c_inprice + ", c_outprice=" + c_outprice + ", c_num=" + c_num + "]";
}
}
PageModel.java
package com.ishopn.model;
import java.util.List;
public class PageModel<E> {
private List<E> list; // 结果集,用于存储查询处理的集合
private int totalRecords; // 查询记录数
private int pageSize; // 每页多少数据
private int pageNo; // 第几页
/**
* 总页数
* @return
*/
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 取得首页
* @return
*/
public int getTopPageNo() {
return 1;
}
/**
* 上一页
* @return
*/
public int getPreviousPageNo() {
if(pageNo <= 1) {
return 1;
}
return pageNo - 1;
}
/**
* 下一页
* @return
*/
public int getNextPageNo() {
if(pageNo >= getBottomPageNo()) {
return getBottomPageNo();
}
return pageNo + 1;
}
/**
* 取得尾页
* @return
*/
public int getBottomPageNo() {
return getTotalPages();
}
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
PageServlet.java
package com.ishopn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CommoditytypeController;
import com.ishopn.model.Commodity;
import com.ishopn.model.Commoditytype;
import com.ishopn.model.PageModel;
import com.ishopn.util.PageHelper;
public class PageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
int pageNo = Integer.parseInt(req.getParameter("pageNo"));
int pageSize = Integer.parseInt(req.getParameter("pageSize"));
PageModel<Commodity> page = PageHelper.findCommodity(pageNo, pageSize);
HttpSession session = req.getSession();
session.setAttribute("pageView", page);
List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
session.setAttribute("ctList", ctList);
resp.sendRedirect("/day_0910_web7/welcome.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
PageHelper.java
package com.ishopn.util;
import java.util.List;
import com.ishopn.dao.impl.CommodityDaoImpl;
import com.ishopn.model.Commodity;
import com.ishopn.model.PageModel;
/**
* 这个类就是用来对PageModel类的填充,返回PageModel的实例对象
* @author Sowhite
*
*/
public class PageHelper {
public static PageModel findCommodity(int pageNo, int pageSize) {
PageModel<Commodity> pageModel = new PageModel<Commodity>();
List<Commodity> list = new CommodityDaoImpl().getCommodityListWithPage(pageNo, pageSize);
int total = new CommodityDaoImpl().getCommodityCount();
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setTotalRecords(total);
pageModel.setList(list);
return pageModel;
}
}
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.ishopn.model.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎界面</title>
<%
List<Commoditytype> ctList = (ArrayList<Commoditytype>)session.getAttribute("ctList");
//List<Commodity> cList = (ArrayList<Commodity>)session.getAttribute("cList");
PageModel p = (PageModel)session.getAttribute("pageView");
// 记录当前页数和页面大小
int pageNo = p.getPageNo();
int pageSize = p.getPageSize();
List<Commodity> cList = p.getList();
%>
</head>
<body>
<h1>欢迎您!<%=session.getAttribute("cu_name") %></h1>
<hr />
<!-- 商品列表的展示 List<Commodity> -->
<form method="post" action="/day_0910_web7/insertCommodityServlet">
<table>
<tr>
<td colspan="1"><input type="text" name="c_name" /></td>
<td><input type="text" name="c_madein" /></td>
<td>
<select name="c_type">
<%
for(Commoditytype ct : ctList)
{
%>
<option value="<%=ct.getCt_id() %>"><%=ct.getCt_name() %></option>
<%
}
%>
</select>
</td>
<td><input type="text" name="c_inprice" /></td>
<td><input type="text" name="c_outprice" /></td>
<td><input type="text" name="c_num" /></td>
<td><input type="submit" name="新增" /></td>
</tr>
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品产地</td>
<td>商品类型</td>
<td>商品进价</td>
<td>商品售价</td>
<td>商品库存</td>
<td>编辑|删除</td>
</tr>
<%
for(Commodity c : cList){
%>
<tr>
<td><%=c.getC_id() %></td>
<td><%=c.getC_name() %></td>
<td><%=c.getC_madein() %></td>
<td><%=c.getCt().getCt_name() %></td>
<td><%=c.getC_inprice() %></td>
<td><%=c.getC_outprice() %></td>
<td><%=c.getC_num() %></td>
<td><a href="/day_0910_web7/toUpdateServlet?c_id=<%=c.getC_id() %>">编辑</a>|<a href="/day_0910_web7/deleteCommodityById?c_id=<%=c.getC_id() %>">删除</a></td>
</tr>
<%
}
%>
<tr>
<td colspan="7">
<%
// 首页的输出
if(p.getTopPageNo() == pageNo){
out.print("首页");
}else{
out.print("<a href='/day_0910_web7/pageServlet?pageNo="+p.getTopPageNo()+"&pageSize="+p.getPageSize()+"'>首页</a>");
}
//上一页的输出
if(p.getPreviousPageNo()==pageNo){
out.print("上一页 ");
}else{
out.print("<a href='/day_0910_web7/pageServlet?pageNo="+p.getPreviousPageNo()+"&pageSize="+p.getPageSize()+"'>上一页</a>");
}
//页数的输出
for(int i=1;i<=p.getTotalPages();i++){
if(i == pageNo){
out.print(i+" ");
continue;
}
out.print("<a href='/day_0910_web7/pageServlet?pageNo="+i+"&pageSize="+p.getPageSize()+"'>"+i+"</a> ");
}
//下一页的输出
if(p.getNextPageNo()==pageNo){
out.print("下一页 ");
}else{
out.print("<a href='/day_0910_web7/pageServlet?pageNo="+p.getNextPageNo()+"&pageSize="+p.getPageSize()+"'>下一页</a>");
}
//尾页的输出
if(p.getBottomPageNo()==pageNo){
out.print("尾页 ");
}else{
out.print("<a href='/day_0910_web7/pageServlet?pageNo="+p.getBottomPageNo()+"&pageSize="+p.getPageSize()+"'>尾页</a>");
}
%>
<select onchange="javascript:location.href='/day_0910_web7/pageServlet?pageNo=1&pageSize='+this.value">
<option value="5" <%=5==pageSize?"selected":"" %>>每页5条记录</option>
<option value="10" <%=10==pageSize?"selected":"" %>>每页10条记录</option>
<option value="15" <%=15==pageSize?"selected":"" %>>每页15条记录</option>
</select>
</tr>
</table>
</form>
</body>
</html>
效果: