使用Mysql的LIMIT方法实现分页查询
LIMIT查询方法
SELECT TABLE_COLUMN1,TABLE_COLUMN2...FROM TABLE_NAME LIMIT OFFSET rowNum;
分页的详细过程,以及分页所要涉及到的变量
- 每一次都要查询总的数据量,防止在这一次查询的之前总的数据量发生了变化。
一、Entity层
Student.java
package com.zbt.entity;
public class Student {
private int id;
private String firstName;
private String lastName;
private String address;
private String phone;
public Student(){}
public Student(int id,String firstName,String lastName,String address,String phone){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phone = phone;
}
//省略了getters方法和setters方法
@Override
public String toString() {
return "Student{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
Page对象:将一个页面信息全部封装到Page对象中
package com.zbt.entity;
import java.util.List;
public class Page {
public final static int NUM = 5;
private int start;//起始页
private int next;//下一页
private int last;//上一页
private int currentPage;//当前页
private int totalPage;//总页数
private List<Student> studentList;//用户信息
//构造方法
public Page(){}
public Page(int start,int num,List<Student> studentList){
this.start = start;
this.studentList = studentList;
//caculate the last
last = start == 0? start:start-NUM;
// calculate the next
next = start + NUM > num ? start: start+NUM;
// calculate the currentPage
currentPage = start/NUM +1;//start从零开始因此要加1
//calculate the totalPage
totalPage = num % NUM ==