一、分頁顯示含義
每次翻頁的時候只從數據庫裏檢索出本頁需要的數據。
二、分頁顯示優點
- 數據清晰直觀
- 不受數據量限制
- 頁面不再冗長
三、分頁顯示實現
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>
效果: