Javaweb中分页功能的实现

一 完成后端分页类的封装

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个显示)

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现JavaWeb的登录注册功能,你可以按照以下步骤操作: 1. 创建数据库表:首先,你需要在数据库创建一个用户表,包含用户名、密码等字段。 2. 创建JavaBean类:创建一个JavaBean类,用于表示用户对象,包含用户名、密码等属性,并提供相应的getter和setter方法。 3. 创建DAO层:创建一个数据访问对象(DAO)层,用于与数据库进行交互。在DAO层,你可以定义一些方法,如根据用户名查询用户、添加用户等。 4. 创建Servlet:创建一个Servlet类,用于接收用户的请求并进行相应的处理。在登录功能,你可以接收用户输入的用户名和密码,并与数据库的数据进行比对。在注册功能,你可以接收用户输入的用户名和密码,并将其存储到数据库。 5. 编写JSP页面:根据需求,编写登录和注册页面的JSP代码。在登录页面,提供用户名和密码的输入框,并通过表单提交到Servlet进行验证。在注册页面,提供用户名和密码的输入框,并通过表单提交到Servlet进行保存。 6. 配置web.xml:在web.xml文件配置Servlet和相关的URL映射,以及配置数据库连接信息。 7. 运行测试:启动服务器,访问登录和注册页面,并进行相应的操作进行测试。 以上是一个简单的实现方式,你可以根据具体需求进行扩展和优化。希望对你有所帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金州饿霸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值