java中的接口是类吗
258
2023-07-20
Hibernate框架数据分页技术实例分析
本文实例讲述了Hibernate框架数据分页技术。分享给大家供大家参考,具体如下:
1.数据分页机制基本思想:
(1)确定记录跨度,即确定每页显示的记录条数,可根据实际情况而定。
(2)获取记录总数,即获取要显示在页面中的总记录数,其目的是根据该数来确定总的分布数。
(3)确定分页后的总页数。可根据公式:“总页数=(总记录数 - 1) / 每页显示的记录数 + 1”。
(4)根据当前页数显示数据。如果该页数小于1,则使其等于1;如果大于最大页数,则使其等于最大页数。
(5)通过For、While循环语句分布显示查询结果。
2.获取前n条记录:
SQL语法:
SELECT TOP n
FROM table
WHERE ...
ORDER BY ...
例如:获取前4条记录
select top 4 * from car
3.获取分页数据:
String sql = "select top"+pagesize+"* from car where id not
in (select top "+(page-1)*pagesize+"id from car order by id ASC)
order by id ASC
其中参数说明如下:
pagesize:每页显示的记录数
page:当前页数
car:数据表名
4.mysql 数据库分页
MySQL数据库提供了LIMIT函数,利用该函数可轻松实现数据分页。
LIMIT函数用来限制SELECT查询语句返回的行数。
语法:
SELECT ...FROM table
WHERE...
ORDER BY ...
LIMIT [offset], rows
其中参数说明如下:
offset:指定要返回的第一行的偏移量。开始行的偏移量是0。是可选的。
rows:指定返回行的数目。
5.MySQL获取分页数据
/**
*
* @param page 第几页
* @param pagesize 每页显示记录数
* @return 返回结果集
*/
public ResultSet findOrder(int page, int pagesize) {
String strSql = "select * from car order by id limit " + (page - 1)
* pagesize + "," + pagesize + ""; // 定义SQL查询语句
Statement pstmt = null;
ResultSet rs = null; // 定义查询结果集对象
try {
pstmt = conn.createStatement();
rs = pstmt.executeQuery(strSql); // 执行查询语句
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
rs.close();
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return rs; // 返回结果集
}
6.数据分页示例
6.1Paging项目结构:
6.2Car.java程序清单:
package com.cdd.util;
/**
* 车辆信息
* @author Xu Qiao Hui
*
*/
public class Car {
private String Id;
private String name;;
private String brand;
private String engineNum;
private String state;
private String remarks;
public Car(int size){}
public Car(){}
public Car(String id, String name, String brand, String engineNum,
String state, String remarks) {
super();
Id = id;
this.name = name;
this.brand = brand;
this.engineNum = engineNum;
this.state = state;
this.remarks = remarks;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getEngineNum() {
return engineNum;
}
public void setEngineNum(String engineNum) {
this.engineNum = engineNum;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
}
6.3GwFbNnXodnetConn.java程序清单:
package com.cdd.util;
import java.sql.*;
public class GetConn {
static {
try {
Class.forName("com.mysql.jdbc.Driver"); // 静态块中实现加载数据库驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConn() {
Connection connection = null;
String url = "jdbc:mysql://localhost:3306/oa";
String userName = "root";
String passWord = "1120";
try {
connection = DriverManager.getConnection(url, userName, passWord);
System.out.println("ok");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) {
GetConn getConn = new GetConn();
getConn.getConn();
}
}
6.4PaginationUtil.java程序清单:
package com.cdd.util;
import java.util.*;
import java.sql.*;
public class PaginationUtil {
GetConn getConn = new GetConn();
Connection conn = getConn.getConn();
// 根据分页
public List findGrade(int page, int pagesize) {
String strSql = "select * from car order by id limit " + (page - 1)
* pagesize + "," + pagesize + ""; // 定义SQL查询语句
Statement pstmt = null;
ResultSet rs = null; // 定义查询结果集对象
List lstList = new ArrayList(); // 定义集合对象
try {
pstmt = conn.createStatement();
rs = pstmt.executeQuery(strSql); // 执行查询语句
while (rs.next()) { // 循环遍历查询结果集
Car car = new Car(); // 创建car
car.setId(rs.getString("Id"));
car.setName(rs.getString("name"));
car.setBrand(rs.getString("brand"));
car.setEngineNum(rs.getString("engineNum"));
car.setState(rs.getString("state"));
car.setRemarks(rs.getString("remarks"));
lstList.add(car); // 向集合中添加对象
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
rs.close();
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return lstList; // 返回查询集合对象
}
/**
*
* @param page 第几页
* @param pagesize 每页显示记录数
* @return 返回结果集
*/
public ResultSet findOrder(int page, int pagesize) {
String strSql = "select * from car order by id limit " + (page - 1)
* pagesize + "," + pagesize + ""; // 定义SQL查询语句
Statement pstmt = null;
ResultSet rs = null; // 定义查询结果集对象
try {
pstmt = conn.createStatement();
rs = pstmt.executeQuery(strSql); // 执行查询语句
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
rs.close();
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return rs; // 返回结果集
}
public int allPage(int pagesize) {
int allp = 0;
try {
Statement pstmt = conn.createStatement();
pstmt.execute("select count(*) from car");
ResultSet rs = pstmt.getResultSet();
System.out.print("00");
rs.next();
int all = rs.getInt(1);
System.out.print(all);
allp = (all - 1) / pagesize + 1;
System.out.println(allp);
} catch (SQLException e) {
e.printStackTrace();
}
return allp;
}
public static void main(String[] args) {
PaginationUtil pageinationUtil = new PaginationUtil();
List list = pageinationUtil.findGrade(2, 6);
for (int i = 0; i < list.size(); i++) {
Car car = (Car) list.get(i);
System.out.println(car.getId() + " " + car.getName());
}
}
}
6.5index.jsp程序清单:
<%@ page language="java" import="java.util.*,com.cdd.util.*;"
pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~