一 完成后端分页类的封装
1. 后端分页功能的基本算法思想:
参数准备:
public class Pager<T> {
private int page = 1;// 当前页号
private int limit = 10;// 每页显示的记录数
private int totalRecord = 0;// 存放总共有多少条记录要显示-sql计算出来
private int totalPage;// 总的页数
private List<T> list;// 取得当前待显示页的数据集合
}
假如有19条商品记录,每一页显示6条记录,总共有4页19/6+1=4 (公式)
1[0 5] 2[6 11] 3[12 17] 4[18 末尾]
首页page=1 limit=6
上一页 page-1 limit=6
下一页 page+1 limit=6
尾页 page=4 limit=6(limit|totalRecord%limit)
对应的查询语句:
1[0 5] 2[6 11] 3[12 17] 4[18 末尾]
If(如果当前处于首页,不应该有上一页和首页)
首页SELECT * FROM tb_product LIMIT 0,limit
如果超过一页,第二个参数=limit,否则=记录数
页号在[1 totalPage]
上一页page=page-1 limit=6
Select * from … limit (页号-1)*limit,limit
下一页 page+1 limit=6
尾页 SELECT * FROM tb_product LIMIT (page-1)*limit, totalRecord%limit
18=(4-1)*6 19%6
完整代码:
public Pager(int page, int limit, int totalRecord) {
this.limit = limit;
this.totalRecord = totalRecord;
this.totalPage = totalRecord % limit == 0 ? totalRecord / limit : totalRecord / limit + 1;
// 判断page合法性
if (page < 1) // 已经是第一页了,上一页
this.page = 1; // 不允许page变化,定在第一页
if (page > totalPage) // 已经是最后一页了,没有下一页
this.page = totalPage; // 定在最后一页,不允许往下加一页
else
this.page = page;
}
public Pager(int page, int totalRecord) {
this.totalRecord = totalRecord;
this.totalPage = totalRecord % limit == 0 ? totalRecord / limit : totalRecord / limit + 1;
// 判断page合法
if (page < 1)
this.page = 1;
if (page > totalPage)
this.page = totalPage;
else
this.page = page;
}
2 实体类Product:
package cn.domain;
import java.util.Date;
public class ProductDomain {
private int id;
private int categoryMainId;
private int categorBranchId;
private String name;
private String producingArea;
private String description;
private Date createTime;
private double marketPrice;
private double sellPrice;
private int salesVolume;
private String picture;
private int discount;
private int stock;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCategoryMainId() {
return categoryMainId;
}
public void setCategoryMainId(int categoryMainId) {
this.categoryMainId = categoryMainId;
}
public int getCategorBranchId() {
return categorBranchId;
}
public void setCategorBranchId(int categorBranchId) {
this.categorBranchId = categorBranchId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProducingArea() {
return producingArea;
}
public void setProducingArea(String producingArea) {
this.producingArea = producingArea;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public double getMarketPrice() {
return marketPrice;
}
public void setMarketPrice(double marketPrice) {
this.marketPrice = marketPrice;
}
public double getSellPrice() {
return sellPrice;
}
public void setSellPrice(double sellPrice) {
this.sellPrice = sellPrice;
}
public int getSalesVolume() {
return salesVolume;
}
public void setSalesVolume(int salesVolume) {
this.salesVolume = salesVolume;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return "ProductDomain [id=" + id + ", categoryMainId=" + categoryMainId + ", categorBranchId=" + categorBranchId
+ ", name=" + name + ", producingArea=" + producingArea + ", description=" + description
+ ", createTime=" + createTime + ", marketPrice=" + marketPrice + ", sellPrice=" + sellPrice
+ ", salesVolume=" + salesVolume + ", picture=" + picture + ", discount=" + discount + ", stock="
+ stock + "]";
}
public ProductDomain(int id, int categoryMainId, int categorBranchId, String name, String producingArea,
String description, Date createTime, double marketPrice, double sellPrice, int salesVolume, String picture,
int discount, int stock) {
super();
this.id = id;
this.categoryMainId = categoryMainId;
this.categorBranchId = categorBranchId;
this.name = name;
this.producingArea = producingArea;
this.description = description;
this.createTime = createTime;
this.marketPrice = marketPrice;
this.sellPrice = sellPrice;
this.salesVolume = salesVolume;
this.picture = picture;
this.discount = discount;
this.stock = stock;
}
public ProductDomain() {
super();
}
}
3ProductDao和ProductImp:
package cn.dal;
import cn.util.Pager;
public interface ProductDao {
Pager find(int page);// 初始值为limit=10
Pager find(int page, int limit);
int getTotalRecord();// 获得总记录个数
}
package cn.dal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import cn.domain.ProductDomain;
import cn.util.DBConnection;
import cn.util.Pager;
public class ProductDaoImp implements ProductDao {
private Connection connection = DBConnection.getConnection();
public Pager find(int page) {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<ProductDomain> list = new ArrayList<ProductDomain>();
Pager pager = new Pager(page, getTotalRecord());
String sql = "select * from tb_product limit ?,?";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, (page - 1) * pager.getLimit());
int tempLimit = 0;
if (page > pager.getTotalRecord() / pager.getLimit())
tempLimit = pager.getTotalRecord() % pager.getLimit();
else
tempLimit = pager.getLimit();
preparedStatement.setInt(2, tempLimit);
resultSet = preparedStatement.executeQuery();
ProductDomain product = null;
while (resultSet.next()) {
product = new ProductDomain(resultSet.getInt("id"), resultSet.getInt(2),
resultSet.getInt(3), resultSet.getString(4),
resultSet.getString(5), resultSet.getString(6),
resultSet.getDate(7), resultSet.getDouble(8),
resultSet.getDouble(9),resultSet.getInt(10),
resultSet.getString(11), resultSet.getInt(12),
resultSet.getInt(13));
list.add(product);
}
pager.setList(list);
} catch (SQLException e) {
e.printStackTrace();
}
return pager;
}
public Pager find(int page, int limit) {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<ProductDomain> list=new ArrayList<ProductDomain>();
Pager pager=new Pager(page,limit,getTotalRecord());
String sql="select * from tb_product limit ?,?";
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, (page-1)*pager.getLimit());
int tempLimit=0;
if(page>pager.getTotalRecord()/pager.getLimit())
tempLimit=pager.getTotalRecord()%pager.getLimit();
else tempLimit=pager.getLimit();
preparedStatement.setInt(2,tempLimit );
resultSet=preparedStatement.executeQuery();
ProductDomain product=null;
while(resultSet.next()){
product = new ProductDomain(resultSet.getInt("id"), resultSet.getInt(2),
resultSet.getInt(3), resultSet.getString(4),
resultSet.getString(5), resultSet.getString(6),
resultSet.getDate(7), resultSet.getDouble(8),
resultSet.getDouble(9),resultSet.getInt(10),
resultSet.getString(11), resultSet.getInt(12),
resultSet.getInt(13));
list.add(product);
}
pager.setList(list);
} catch (SQLException e) {
e.printStackTrace();
}
return pager;
}
public int getTotalRecord() {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int result = 0; // mysql中总的记录数
String sql = "SELECT COUNT(*) AS number FROM tb_product"; // 通过mysql语句记录总的记录数
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
if (resultSet.next())
result = resultSet.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
4 测试类:
@Test
public void test0() {
ProductDao dao = new ProductDaoImp();
/*System.out.println(dao.getTotalRecord());*/
Pager pager = dao.find(1, 6); //第一页显示6条记录
List<ProductDomain> list = pager.getList(); //19
for(int i=0; i<list.size(); i++)
System.out.println(list.get(i).toString());
}
结果:显示第一页的结果(一页按6个显示)