建表及准备数据

创建实体类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}">«</a></li>
</c:if>
<c:if test="${pageBean.currentPage != 1 }">
<li><a href="${pageContext.request.contextPath }/findUser?currentPage=${pageBean.currentPage-1}">«</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}">»</a></li>
</c:if>
<c:if test="${pageBean.currentPage == pageBean.totalPage }">
<li class="disabled"><a href="${pageContext.request.contextPath }/findUser?currentPage=${pageBean.currentPage+1}">»</a></li>
</c:if>
</ul>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>
测试:



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

被折叠的 条评论
为什么被折叠?



