【java基础】jdbc的简单分页查询

1.创建分页实体类

public class PageBean<T> {
	private List<T> list;// 当前页内容 查询
	private int currPage;// 当前页码 传递
	private int pageSize;// 每页显示的条数 固定
	private int totalCount;// 总条数 查询
	private int totalPage;// 总页数 计算
	。。。。。
	/**
	 * 获取总页数
	 * 
	 * @return
	 */
	public int getTotalPage() {
		return (int) Math.ceil(totalCount * 1.0 / pageSize);
	}


	public PageBean() {
	}


	public PageBean(List<T> list, int currPage, int pageSize, int totalCount) {
		super();
		this.list = list;
		this.currPage = currPage;
		this.pageSize = pageSize;
		this.totalCount = totalCount;
	}
}



2.service调Dao分别获取查询当页的数据list和总数count

将list和count等数据封装到PageBean中


3.servlet通过service获取数据返回到前台展示

分页部分:

<center>
		<!-- 若是第一页,首页和上一页不显示 -->
		<c:if test="${pb.currPage!=1 }">
			<a
				href="${pageContext.request.contextPath }/showProductsByPage?currPage=1">[首页]</a>
			<a
				href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.currPage-1}">[上一页]</a>
		</c:if>

		<!-- 将所有的页码显示出来 -->
		<c:forEach begin="1" end="${pb.totalPage }" var="n">
			<c:if test="${pb.currPage==n }">
				${n }
			</c:if>
			<c:if test="${pb.currPage!=n }">
				<a
					href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.currPage}">${n }</a>
			</c:if>
		</c:forEach>

		<!-- 若是最后一页,末页和下一页不显示 -->
		<c:if test="${pb.currPage!=pb.totalPage }">
			<a
				href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.currPage+1}">[下一页]</a>
			<a
				href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.totalPage}">[末页]</a>

		</c:if>
	</center>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值