1、数据库分页技术的基本思想:
(1)、确定记录跨度。即确定每页显示数据的条数。
(2)、获取记录总数。即获取要显示在页面中的总记录数。其目的是依据总记录数来技术得到总页数。
(3)、确定分页后的总页数。依据公式“总记录数/跨度”计算分页后的总页数。要注意的是:如果总页数有余数,要去掉余数,将总页数加1。修改为:(总记录数-1)/跨度+1
(4)、根据当前页数显示数据。注意:如果该页数小于1,则使其等于1;如果页数大于最大页数,则使其等于最大页数。
(5)、通过For循环语句分页显示数据。
2、使用标准的SQL语句实现数据分页:
(1)、获取前n条数据:
SELECT TOP n[PERCENT] *
FROM TABLE
WHERE ......
ORDER BY......
(2)、获取分页数据:
在java中通过参数来获取分页数据:
String sql="select top "+pageSize+"* from table where id not in (select top"+(page-1)*pageSize+" id from table order by id ASC ) order by id ASC";
其中:
pageSize:英语指定分页后每页显示的数据
page:用于指定当前页数
3、MySQL数据库分页:
MySQL数据库提供了LIMIT函数课一方便实现分页:
SELECT [DISTINCT|UNIQUE]( *,columname [as alias])
FROM TABLE
WHERE ......
ORDER BY......
LIMIT [offset],rows
eg:
String strSql = "select * from tb_orderform order by id limit "+(page-1)*pagesize+","+pagesize+"";
4、Hibernate分页:
Hibernate对分页也提供了很好的支持,可以利用HQL和QBC检索方式实现数据分页。
eg:从索引位置3开始的6条记录。
(1)、HQL方式:
Query query=session.createQuery("from User");
query.setFirstResult(3);
query.setMaxResult(6);
(2)、QBC方式:
Criteria criteria=session.createCriteria(User.class);
createria.setFirstResult(3);
createria.setMaxResult(6);
5、实践:
(1)、实体类:
1
package com.domain;
2
3
import org.apache.struts.action.ActionForm;
4
5
public
class PeopleForm
extends ActionForm
{
6
private int id;
7
private String name;
8
private String sex ;
9
private int age;
10
private String job;
11
public int getId() {
12
return id;
13
}
14
public void setId(
int id) {
15
this.id = id;
16
}
17
public String getName() {
18
return name;
19
}
20
public void setName(String name) {
21
this.name = name;
22
}
23
public String getSex() {
24
return sex;
25
}
26
public void setSex(String sex) {
27
this.sex = sex;
28
}
29
public int getAge() {
30
return age;
31
}
32
public void setAge(
int age) {
33
this.age = age;
34
}
35
public String getJob() {
36
return job;
37
}
38
public void setJob(String job) {
39
this.job = job;
40
}
41
42
43
}
44
(2)、Dao:
数据库操作:
1
package com.dao;
2
import java.sql.*;
3
public
class JDBConnection
{
4
Connection connection =
null;
5
static {
6
try {
7
Class.forName("com.mysql.jdbc.Driver");
// 静态块中实现加载数据库驱动
8
}
catch (ClassNotFoundException e) {
9
e.printStackTrace();
10
}
11
}
12
public Connection creatConnection(){
13
//创建数据库连接对象
14
String url = "jdbc:mysql://localhost:3306/db_database20";
//指定数据库连接URL
15
String userName = "root";
//连接数据库用户名
16
String passWord = "111";
//连接数据库密码
17
try {
18
connection = DriverManager.getConnection(url,userName, passWord);
//获取数据库连接
19
}
catch (SQLException e) {
20
e.printStackTrace();
21
}
22
return connection;
23
}
24
//对数据库的查询操作
25
public ResultSet executeQuery(String sql) {
26
ResultSet rs;
//定义查询结果集
27
try {
28
if (connection ==
null) {
29
creatConnection();
//创建数据库连接
30
}
31
Statement stmt = connection.createStatement();
//创建Statement对象
32
rs = stmt.executeQuery(sql);
//执行查询SQL语句
33
}
catch (SQLException e) {
34
System.out.println(e.getMessage());
35
return null;
//有异常发生返回null
36
}
37
return rs;
//返回查询结果集对象
38
}
39
//关闭数据库连接
40
public void closeConnection() {
41
if (connection !=
null) {
//如果Connection对象
42
try {
43
connection.close();
//关闭连接
44
}
catch (SQLException e) {
45
e.printStackTrace();
46
}
finally {
47
connection =
null;
48
}
49
}
50
}
51
52
}
53
业务逻辑:
1
package com.dao;
2
import java.util.*;
3
import com.domain.PeopleForm;
4
import java.sql.ResultSet;
5
import java.sql.*;
6
public
class PeopleDao
{
7
private JDBConnection connection =
null;
8
public PeopleDao() {
9
connection =
new JDBConnection();
10
}
11
//查询所有员工信息方法
12
public List selectPeople() {
13
List list =
new ArrayList();
//创建保存查询结果集集合对象
14
PeopleForm form =
null;
15
String sql = "select * from tb_emp";
//定义查询tb_emp表中全部数据SQL语句
16
ResultSet rs = connection.executeQuery(sql);
//执行查询
17
try {
18
while (rs.next()) {
//循环遍历查询结果集
19
form =
new PeopleForm();
//创建ActionForm实例
20
form.setId(Integer.valueOf(rs.getString(1)));
//获取查询结果
21
form.setName(rs.getString(2));
22
form.setSex(rs.getString(3));
23
form.setAge(rs.getInt(4));
24
form.setJob(rs.getString(5));
25
list.add(form);
//向集合中添加对象
26
}
27
}
catch (SQLException ex) {
28
}
29
connection.closeConnection();
//关闭数据连接
30
return list;
//返回查询结果
31
}
32
}
33
(3)Action:
1
package com.action;
2
3
import org.apache.struts.action.*;
4
import javax.servlet.http.*;
5
import com.dao.PeopleDao;
6
import java.util.List;
7
public
class PeopleAction
extends Action
{
8
private PeopleDao dao =
null;
9
public ActionForward execute(ActionMapping mapping, ActionForm form,
10
HttpServletRequest request, HttpServletResponse response) {
11
dao =
new PeopleDao();
// 创建保存有数据查询类对象
12
List list = dao.selectPeople();
// 调用数据查询方法
13
int pageNumber = list.size();
// 计算出有多少条记录
14
int maxPage = pageNumber;
// 计算有多少页数
15
String number = request.getParameter("i");
// 获取保存在request对象中变量
16
if (maxPage % 4 == 0) {
// “4”代表每页显示有4条记录
17
maxPage = maxPage / 4;
// 计算总页数
18
}
else {
// 如果总页数除以4不整除
19
maxPage = maxPage / 4 + 1;
// 将总页数加1
20
}
21
if (number ==
null) {
// 如果保存在request范围内的当前页数为null
22
number = "0";
// 将number为0
23
}
24
request.setAttribute("number", String.valueOf((number)));
// 将number保存在request范围内
25
request.setAttribute("maxPage", String.valueOf(maxPage));
// 将分的总页数保存在request范围内
26
int nonce = Integer.parseInt(number) + 1;
27
request.setAttribute("nonce", String.valueOf(nonce));
28
request.setAttribute("pageNumber", String.valueOf(pageNumber));
29
request.setAttribute("list", list);
30
return mapping.findForward("peopleAction");
// 请求转发地址
31
}
32
}
33
(4)、页面代码:

index.jsp:

<%@ page language="java"
import="java.util.*" pageEncoding="gbk"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<meta http-equiv="refresh" content="0;URL=peopleAction.do">

</head>

<body>

</body>

</html>

===============================================

pagenation.jsp

<%@ page contentType="text/html; charset=gbk" %>

<%@page
import="java.sql.*"%>

<%@page
import="java.util.*"%>

<%@page
import="com.domain.PeopleForm"%>

<html>

<meta http-equiv="Content-Type" content="text/html;charset=gbk">

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

<link href="css/style.css" rel="stylesheet" type="text/css">

<%

List list = (List)request.getAttribute("list");
//
获取保存在request范围内的数据
int number = Integer.parseInt((String)request.getAttribute("number"));
int maxPage = Integer.parseInt((String)request.getAttribute("maxPage"));
int pageNumber = Integer.parseInt((String)request.getAttribute("pageNumber"));
int start = number*4;
//
开始条数
int over = (number+1)*4;
//
结束条数
int count=pageNumber-over;
//
还剩多少条记录
if(count<=0)
{

over=pageNumber;

}

%>

<head>

<title>利用查询结果集进行分页</title>

</head>

<body >

<table width="756" height="650" border="0" align="center" cellpadding="0"

cellspacing="0" >

<tr>

<td height="280">

<table width="635" border="1" align="center">

<tr align="center" bgcolor="#FFFFFF">

<td width="112" height="17"><span
class="style4">编号</span></td>

<td width="112"><span
class="style4">姓名</span></td>

<td width="112"><span
class="style4">性别</span></td>

<td width="112"><span
class="style4">年龄</span></td>

<td width="142"><span
class="style4">职位</span></td>

</tr>

<logic:iterate id="element" indexId="index" name="list"

offset="<%=String.valueOf(start)%>"

length="4"> <!-- 通过迭代标签将员工信息输出 -->

<tr align="center" bgcolor="#FFFFFF">

<td height="22">

<bean:write name="element" property="id"/>

</td>

<td>

<bean:write name="element" property="name"/>

</td>

<td><bean:write name="element" property="sex"/>

</td>

<td><bean:write name="element" property="age"/>岁</td>

<td><bean:write name="element" property="job"/>

</td>

</tr>

</logic:iterate>

</table>

</td>

</tr>

<tr>

<td valign="top">

<form name="form" method="post" action="peopleAction.do">

<table width="400" height="20" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td width="400" valign="middle" bgcolor="#CCCCCC">

共为<bean:write name="maxPage"/> <!-- 输出总记录数 -->

页 共有<bean:write name="pageNumber"/> <!-- 输出总分页 -->

条 当前为第<bean:write name="nonce"/>页 <!-- 输出当前页数 -->

<logic:equal name="nonce" value="1">

首页

</logic:equal>

<logic:notEqual name="nonce" value="1"> <!-- 如果当前页码不等于1 -->

<a href="peopleAction.do?i=0">首页</a> <!-- 提供首页超链接 -->

</logic:notEqual>


<logic:lessEqual name="maxPage" value="${nonce}"> <!-- 如果当前页码不小于总页数 -->

尾页 <!-- 不提供尾页超链接 -->

</logic:lessEqual>

<logic:greaterThan name="maxPage" value="${nonce}"> <!-- 如果当前页码小于总页数 -->

<a href="peopleAction.do?i=<%=maxPage-1%>">尾页</a> <!-- 提供尾页超链接 -->

</logic:greaterThan>

<logic:equal name="nonce" value="1"> <!-- 如果当前页码等于1 -->

上一页 <!-- 不提供上一页超链接 -->

</logic:equal>

<logic:notEqual name="nonce" value="1"> <!-- 如果当前页码不等于1 -->

<a href="peopleAction.do?i=<%=number-1%>">上一页</a> <!-- 提供上一页超链接 -->

</logic:notEqual>

<logic:lessEqual name="maxPage" value="${nonce}">

下一页

</logic:lessEqual>

<logic:greaterThan name="maxPage" value="${nonce}"> <!-- 如果当前页面小于总页数 -->

<a href="peopleAction.do?i=<%=number+1%>">下一页</a> <!-- 提供下一页超链接 -->

</logic:greaterThan>

</td>

</tr>

</table> </form></td>

</tr>

</table>

</body>

</html>
(5)、Struts-config.xml
1

<?xml version="1.0" encoding="UTF-8"?>
2

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
3
4

"http://struts.apache.org/dtds/struts-config_1_2.dtd">
5
6

<struts-config>
7

<data-sources />
8

<form-beans>
9

<form-bean name="peopleForm" type="com.domain.PeopleForm" />
10

</form-beans>
11

<global-exceptions />
12

<global-forwards />
13

<action-mappings >
14

<action name="peopleForm" path="/peopleAction" scope="request"
15

type="com.action.PeopleAction" validate="true">
16

<forward name="peopleAction" path="/pagination.jsp" />
17

</action>
18

</action-mappings>
19

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
20

</struts-config>
21