使用hibernate和struts2實現分頁功能的示例
想著每天能學個新東西,今天準備了這個hibernate+struts2實現頁面的分頁功能,以下是源代碼。
1.DAO層接口的設計,定義一個PersonDAO接口,里面聲明了兩個方法:
public interface PersonDAO
{
public List<Person> queryByPage(String hql, int offset, int pageSize);
public int getAllRowCount(String hql);
}
2.DAO層接口的實現類PersonDAOImpl類,將其兩個方法實現出來:
public class PersonDAOImpl implements PersonDAO
{
/**
* 通過hql語句得到數據庫中記錄總數
*/
@Override
public int getAllRowCount(String hql)
{
Session session = HibernateUtil.openSession();
Transaction tx = null;
int allRows = 0;
try
{
tx = session.beginTransaction();
Query query = session.createQuery(hql);
allRows = query.list().size();
tx.commit();
}
catch (Exception e)
{
if(tx != null)
{
tx.rollback();
}
e.printStackTrace();
}
finally
{
HibernateUtil.closeSession(session);
}
return allRows;
}
/**
* 使用hibernate提供的分頁功能,得到分頁顯示的數據
*/
@SuppressWarnings("unchecked")
@Override
public List<Person> queryByPage(String hql, int offset, int pageSize)
{
Session session = HibernateUtil.openSession();
Transaction tx = null;
List<Person> list = null;
try
{
tx = session.beginTransaction();
Query query = session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize);
list = query.list();
tx.commit();
}
catch (Exception e)
{
if(tx != null)
{
tx.rollback();
}
e.printStackTrace();
}
finally
{
HibernateUtil.closeSession(session);
}
return list;
}
}
3.定義了一個PageBean(每一頁所需要的內容都存放在這個PageBean里面),里面用來存放網頁每一頁顯示的內容:
public class PageBean
{
private List<Person> list; //通過hql從數據庫分頁查詢出來的list集合
private int allRows; //總記錄數
private int totalPage; //總頁數
private int currentPage; //當前頁
public List<Person> getList()
{
return list;
}
public void setList(List<Person> list)
{
this.list = list;
}
public int getAllRows()
{
return allRows;
}
public void setAllRows(int allRows)
{
this.allRows = allRows;
}
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;
}
/**
* 得到總頁數
* @param pageSize 每頁記錄數
* @param allRows 總記錄數
* @return 總頁數
*/
public int getTotalPages(int pageSize, int allRows)
{
int totalPage = (allRows % pageSize == 0)? (allRows / pageSize): (allRows / pageSize) + 1;
return totalPage;
}
/**
* 得到當前開始記錄號
* @param pageSize 每頁記錄數
* @param currentPage 當前頁
* @return
*/
public int getCurrentPageOffset(int pageSize, int currentPage)
{
int offset = pageSize * (currentPage - 1);
return offset;
}
/**
* 得到當前頁, 如果為0 則開始第一頁,否則為當前頁
* @param page
* @return
*/
public int getCurPage(int page)
{
int currentPage = (page == 0)? 1: page;
return currentPage;
}
}
4.Service層接口設計,定義一個PersonService接口,里面聲明了一個方法,返回一個PageBean:
public interface PersonService
{
public PageBean getPageBean(int pageSize, int page);
}
5.Service層接口實現類PersonServiceImpl類,實現唯一的方法:
public class PersonServiceImpl implements PersonService
{
private PersonDAO personDAO = new PersonDAOImpl();
/**
* pageSize為每頁顯示的記錄數
* page為當前顯示的網頁
*/
@Override
public PageBean getPageBean(int pageSize, int page)
{
PageBean pageBean = new PageBean();
String hql = "from Person";
int allRows = personDAO.getAllRowCount(hql);
int totalPage = pageBean.getTotalPages(pageSize, allRows);
int currentPage = pageBean.getCurPage(page);
int offset = pageBean.getCurrentPageOffset(pageSize, currentPage);
List<Person> list = personDAO.queryByPage(hql, offset, pageSize);
pageBean.setList(list);
pageBean.setAllRows(allRows);
pageBean.setCurrentPage(currentPage);
pageBean.setTotalPage(totalPage);
return pageBean;
}
}
6.Action層設計,定義一個PersonAction:
public class PersonAction extends ActionSupport
{
private PersonService personService = new PersonServiceImpl();
private int page;
public int getPage()
{
return page;
}
public void setPage(int page)
{
this.page = page;
}
@Override
public String execute() throws Exception
{
//表示每頁顯示5條記錄,page表示當前網頁
PageBean pageBean = personService.getPageBean(5, page);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("pageBean", pageBean);
return SUCCESS;
}
}
7.輔助類設計,HibernateUtil:
public class HibernateUtil
{
private static SessionFactory sessionFactory;
static
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static Session openSession()
{
Session session = sessionFactory.openSession();
return session;
}
public static void closeSession(Session session)
{
if(session != null)
{
session.close();
}
}
}
8.最后也就是分頁頁面顯示pagePerson.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'pagePerson.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
function validate()
{
var page = document.getElementsByName("page")[0].value;
if(page > <s:property value="#request.pageBean.totalPage"/>)
{
alert("你輸入的頁數大于最大頁數,頁面將跳轉到首頁!");
window.document.location.href = "personAction";
return false;
}
return true;
}
</script>
</head>
<body>
<h1><font color="blue">分頁查詢</font></h1><hr>
<table border="1" align="center" bordercolor="yellow" width="50%">
<tr>
<th>序號</th>
<th>姓名</th>
<th>年齡</th>
</tr>
<s:iterator value="#request.pageBean.list" id="person">
<tr>
<th><s:property value="#person.id"/></th>
<th><s:property value="#person.name"/></th>
<th><s:property value="#person.age"/></th>
</tr>
</s:iterator>
</table>
<center>
<font size="5">共<font color="red"><s:property value="#request.pageBean.totalPage"/></font>頁 </font>
<font size="5">共<font color="red"><s:property value="#request.pageBean.allRows"/></font>條記錄</font><br><br>
<s:if test="#request.pageBean.currentPage == 1">
首頁 上一頁
</s:if>
<s:else>
<a href="personAction.action">首頁</a>
<a href="personAction.action?page=<s:property value="#request.pageBean.currentPage - 1"/>">上一頁</a>
</s:else>
<s:if test="#request.pageBean.currentPage != #request.pageBean.totalPage">
<a href="personAction.action?page=<s:property value="#request.pageBean.currentPage + 1"/>">下一頁</a>
<a href="personAction.action?page=<s:property value="#request.pageBean.totalPage"/>">尾頁</a>
</s:if>
<s:else>
下一頁 尾頁
</s:else>
</center><br>
<center>
<form action="personAction" onsubmit="return validate();">
<font size="4">跳轉至</font>
<input type="text" size="2" name="page">頁
<input type="submit" value="跳轉">
</form>
</center>
</body>
</html>
至此,hibernate+struts2實現網頁分頁功能代碼部分就完畢了,像hibernate與struts的配置文件就不列出來了,那些都不是重點!
頁面效果如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Java框架篇:Spring+SpringMVC+hibernate整合開發(fā)
- Hibernate緩存詳解
- java Hibernate多對多映射詳解及實例代碼
- 淺談SpringMVC+Spring3+Hibernate4開發(fā)環(huán)境搭建
- Java Hibernate對象(瞬時態(tài),持久態(tài),脫管態(tài))詳解
- Spring與Hibernate整合事務管理的理解
- Maven 搭建SpringMVC+Hibernate項目詳解
- Java的Hibernate框架中復合主鍵映射的創(chuàng)建和使用教程
- Java的Hibernate框架結合MySQL的入門學習教程
- 詳解Hibernate cascade級聯屬性的CascadeType的用法
相關文章
Spring Security+Spring Data Jpa如何進行安全管理
這篇文章主要介紹了Spring Security+Spring Data Jpa如何進行安全管理,幫助大家更好的理解和學習Spring Security框架,感興趣的朋友可以了解下2020-09-09
IDEA2021.2配置docker如何將springboot項目打成鏡像一鍵發(fā)布部署
這篇文章主要介紹了IDEA2021.2配置docker如何將springboot項目打成鏡像一鍵發(fā)布部署,本文圖文實例相結合給大家介紹的非常詳細,需要的朋友可以參考下2021-09-09

