Web--简单实现分页查询

本文详细介绍了一种基于Hibernate框架的用户数据分页查询实现方案,包括实体类、元数据配置、分页Bean、控制层、Service层、Dao层及前端页面的详细代码示例。
建表及准备数据

在这里插入图片描述

创建实体类User:
package pers.zhang.domain;

public class User {
	
	private Long user_id;
	private String user_code;
	private String user_name;
	private String user_password;
	private String user_state;
	public Long getUser_id() {
		return user_id;
	}
	public void setUser_id(Long user_id) {
		this.user_id = user_id;
	}
	public String getUser_code() {
		return user_code;
	}
	public void setUser_code(String user_code) {
		this.user_code = user_code;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getUser_password() {
		return user_password;
	}
	public void setUser_password(String user_password) {
		this.user_password = user_password;
	}
	public String getUser_state() {
		return user_state;
	}
	public void setUser_state(String user_state) {
		this.user_state = user_state;
	}
}
配置Hibernate元数据User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pers.zhang.domain" >
	<class name="User" table="sys_user" >
		<id name="user_id"  >
			<generator class="identity"></generator>
		</id>
		<property name="user_code" column="user_code" ></property>
		<property name="user_name" column="user_name" ></property>
		<property name="user_password" column="user_password" ></property>
		<property name="user_state" column="user_state" ></property>
	</class>
</hibernate-mapping>
PageBean:
package pers.zhang.domain;

import java.util.ArrayList;
import java.util.List;

public class PageBean<T> {

	//每页需要显示的数据
	private List<T> userList = new ArrayList<T>();
	
	//总页数
	private int totalPage;
	
	//当前页
	private int currentPage;
	
	//总条数
	private int totalCount;
	
	//当前页显示条数
	private int currentCount;

	public List<T> getUserList() {
		return userList;
	}

	public void setUserList(List<T> userList) {
		this.userList = userList;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getCurrentCount() {
		return currentCount;
	}

	public void setCurrentCount(int currentCount) {
		this.currentCount = currentCount;
	}
}
控制层:
package pers.zhang.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pers.zhang.domain.PageBean;
import pers.zhang.domain.User;
import pers.zhang.service.UserService;


public class FindAllUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		UserService userService = new UserService();
		PageBean<User> pageBean = null;
		
		
		//获取当前是第几页
		String currentPageStr = request.getParameter("currentPage");
		//如果为null,则是第一次访问,赋值1
		if(currentPageStr == null) {
			currentPageStr = "1";
		}
		int currentPage = Integer.parseInt(currentPageStr);
		
		//每页显示6条
		int currentCount = 6;
		
		try {
			//获取pageBean
			pageBean = userService.findPageBean(currentPage,currentCount);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher("list.jsp").forward(request, response);
	}

}
Service层:
package pers.zhang.service;

import java.sql.SQLException;
import java.util.List;

import pers.zhang.dao.UserDao;
import pers.zhang.domain.PageBean;
import pers.zhang.domain.User;

public class UserService {

	
	//封装pageBean并返回
	public PageBean findPageBean(int currentPage, int currentCount) throws SQLException{
		UserDao userDao = new UserDao();
		//获取总条数
		int totalCount = userDao.getUserCount();
		
		
		PageBean pageBean = new PageBean();
		
		//当前页
		pageBean.setCurrentPage(currentPage);
		//每页显示条数
		pageBean.setCurrentCount(currentCount);
		//总条数
		pageBean.setTotalCount(totalCount);
		//总页数
		pageBean.setTotalPage((int)(Math.ceil(1.0 * totalCount / currentCount)));
		//每页显示的数据
		int index = (currentPage - 1) * currentCount;//index为查询开始位置=(当前页-1)*每页显示条数
		List<User> list = userDao.findUserListForPageBean(index, currentCount);
		pageBean.setUserList(list);
		
		return pageBean;
	}
}
Dao层:
package pers.zhang.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

import pers.zhang.domain.User;
import pers.zhang.utils.HibernateUtils;

public class UserDao {


	//获取总条数
	public int getUserCount() {
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();

		Criteria criteria = session.createCriteria(User.class);
		//设置查询的聚合函数 => 总行数
		criteria.setProjection(Projections.rowCount());
		Long count = (Long) criteria.uniqueResult();
		tx.commit();
		session.close();
		return count.intValue();
	}
	
	
	//分页查询
	public List<User> findUserListForPageBean(int index, int currentCount) {
		
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();

		Criteria criteria = session.createCriteria(User.class);
		//设置分页信息 limit ?,?  
		criteria.setFirstResult(index);
		criteria.setMaxResults(currentCount);
		List<User> list = criteria.list();

		tx.commit();
		session.close();
		return list;
	}
}
前端页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
		<script type="text/javascript" src="js/bootstrap.min.js" ></script>
		<link rel="stylesheet" href="css/bootstrap.css" />
	</head>
	<body>
		<div style="text-align: center; width: 600px;">
		<table class="table table-hover">
		  <tr><td>编号</td><td>用戶ID</td><td>用户名</td><td>昵称</td><td>密码</td></tr>
		  <!-- 显示数据 -->
		  <c:forEach items="${pageBean.userList }" var="list">
		  	<tr><td>${list.user_id }</td><td>${list.user_code }</td><td>${list.user_name }</td><td>${list.user_password }</td><td>${list.user_state }</td></tr>
		  </c:forEach>
		  <tr><td colspan="5">
		  	<div>
		  		<ul class="pagination">
		  			<!-- 判断上一页能否被点击 -->
		  			<c:if test="${pageBean.currentPage == 1 }">
		  				<li class="disabled"><a href="${pageContext.request.contextPath }/findUser?currentPage=${pageBean.currentPage-1}">&laquo;</a></li>
		  			</c:if>
		  			<c:if test="${pageBean.currentPage != 1 }">
		  				<li><a href="${pageContext.request.contextPath }/findUser?currentPage=${pageBean.currentPage-1}">&laquo;</a></li>
		  			</c:if>
		  			
		  			<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
		  				<!-- 判断是否为当前页,当前页不可点击 -->
		  				<c:if test="${pageBean.currentPage == page }">
		  					<li class="active"><a href="javascript:void(0);">${page }</a></li>
		  				</c:if>
		  				<c:if test="${pageBean.currentPage != page }">
		  					<li><a href="${pageContext.request.contextPath }/findUser?currentPage=${page}">${page }</a></li>
		  				</c:if>
		  				
		  			</c:forEach>
		  			<!-- 判断下一页能够被点击 -->
		  			<c:if test="${pageBean.currentPage != pageBean.totalPage }">
		  				<li><a href="${pageContext.request.contextPath }/findUser?currentPage=${pageBean.currentPage+1}">&raquo;</a></li>
		  			</c:if>
		  			<c:if test="${pageBean.currentPage == pageBean.totalPage }">
		  				<li class="disabled"><a href="${pageContext.request.contextPath }/findUser?currentPage=${pageBean.currentPage+1}">&raquo;</a></li>
		  			</c:if>
				</ul>
		  	</div>
		  	</td>
		  </tr>
		</table>
	</div>


	</body>
</html>
测试:

在这里插入图片描述
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值