常见的分页实现方式
- 使用List接口的subList(int startIndex, int endIndex)方法实现分页
- 直接使用数据库SQL语句实现分页
- 使用hibernate等 框架实现跨数据库的分页
使用SQL语句实现分页
利用数据库自带的分页语法,使用分页语句,获取分页数据(理入MySQL数据库自带的limit关键字,Oracle中使用rownum关键字等)
举例:从学生表(t_student)中查询出前十条数据
MySQL查询语句:
select * from t_student limit 0,10
Oracle查询语句:子查询
select * from
( select s.*,rownum rn from(select * from t_student) s where rownum <= 10 ) where rn >= 1
使用hibernate等 框架实现跨数据库的分页
创建Query或者Criteria对象,查询时,设置 firstResult (从第几条数据开始查)和 maxResults (一共查询多少条记录)属性
String hql = "from Student"; Query q = session.createQuery(hql); q.setFirstResult(0); q.setMaxResult(10); List list = q.list();
实现方式 | 优点 | 缺点 | 适用场景 |
subList | 简单易用 | 效率低 | 无法按需批量获取数据 |
SQL语句 | 简单直接、效率高 | 数据库兼容差 | 不要求数据库兼容 |
Hibernate框架 | 面向对象、兼容性强 | 复杂查询性能低 | 兼容不同数据库 |
项目实战
以上主要包括几个部分:
- 上面是根据学生姓名、性别查询功能
- 中间是学生信息列表,包含查询出的学生信息
- 左下部分是分页概述,包含有多少条记录、多少页、当前是第几页
- 后面的是分页的几个操作,包括【首页】、【上一页】、【下一页】、【尾页】
因为Java是面向对象语言,我们首先想想如何把它抽象为一个个对象!
每条记录抽象为一个学生对象:
左下角有一些分页的情况:
完整表示出来为:
数据库建表:
MVC构架:
项目结构:
对所用到的数据实体建模:
Student.java & Pager.java
package com.model; import java.io.Serializable; public class Student implements Serializable{ private static final long serialVersionUID = 237085339399857983L; private int id; private String stuName; private int gender; private int age; private String address; public Student() { super(); } public Student(int id, String stuName, int gender, int age, String address) { super(); this.id = id; this.stuName = stuName; this.gender = gender; this.age = age; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getGender() { return gender; } public void setGender(int gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public static long getSerialversionuid() { return serialVersionUID; } @Override public String toString() { return "Student [id=" + id + ", stuName=" + stuName + ", gender=" + gender + ", age=" + age + ", address=" + address + "]"; } }
package com.model; import java.io.Serializable; import java.util.List; public class Pager<T> implements Serializable{ private static final long serialVersionUID = -3610609414268644601L; private int pageSize; //每页显示多少条记录 private int currentPage; //当前第几页数据 private int totalRecord; //一共多少条记录 private int totalPage; //一共多少页记录 private List<T> dataList; //要显示的数据 public Pager() { super(); } public Pager(int pageSize, int currentPage, int totalRecord, int totalPage, List<T> dataList) { super(); this.pageSize = pageSize; this.currentPage = currentPage; this.totalRecord = totalRecord; this.totalPage = totalPage; this.dataList = dataList; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<T> getDataList() { return dataList; } public void setDataList(List<T> dataList) { this.dataList = dataList; } public static long getSerialversionuid() { return serialVersionUID; } @Override public String toString() { return "Pager [pageSize=" + pageSize + ", currentPage=" + currentPage + ", totalRecord=" + totalRecord + ", totalPage=" + totalPage + ", dataList=" + dataList + "]"; } }