最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用hibernate和struts2實(shí)現(xiàn)分頁功能的示例

 更新時(shí)間:2017年01月04日 15:30:19   作者:xiaoluo501395377  
本篇文章主要介紹了使用hibernate和struts2實(shí)現(xiàn)分頁功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

想著每天能學(xué)個(gè)新東西,今天準(zhǔn)備了這個(gè)hibernate+struts2實(shí)現(xiàn)頁面的分頁功能,以下是源代碼。

1.DAO層接口的設(shè)計(jì),定義一個(gè)PersonDAO接口,里面聲明了兩個(gè)方法:

public interface PersonDAO
{
  public List<Person> queryByPage(String hql, int offset, int pageSize);
  
  public int getAllRowCount(String hql);
}

2.DAO層接口的實(shí)現(xiàn)類PersonDAOImpl類,將其兩個(gè)方法實(shí)現(xiàn)出來:

public class PersonDAOImpl implements PersonDAO
{
  /**
   * 通過hql語句得到數(shù)據(jù)庫中記錄總數(shù)
   */
  @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提供的分頁功能,得到分頁顯示的數(shù)據(jù)
   */
  @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.定義了一個(gè)PageBean(每一頁所需要的內(nèi)容都存放在這個(gè)PageBean里面),里面用來存放網(wǎng)頁每一頁顯示的內(nèi)容:

public class PageBean
{
  private List<Person> list; //通過hql從數(shù)據(jù)庫分頁查詢出來的list集合
  
  private int allRows; //總記錄數(shù)
  
  private int totalPage; //總頁數(shù)
  
  private int currentPage; //當(dāng)前頁

  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;
  }
  
  /**
   * 得到總頁數(shù)
   * @param pageSize 每頁記錄數(shù)
   * @param allRows 總記錄數(shù)
   * @return 總頁數(shù)
   */
  public int getTotalPages(int pageSize, int allRows)
  {
    int totalPage = (allRows % pageSize == 0)? (allRows / pageSize): (allRows / pageSize) + 1;
    
    return totalPage;
  }
  
  /**
   * 得到當(dāng)前開始記錄號
   * @param pageSize 每頁記錄數(shù)
   * @param currentPage 當(dāng)前頁
   * @return
   */
  public int getCurrentPageOffset(int pageSize, int currentPage)
  {
    int offset = pageSize * (currentPage - 1);
    
    return offset;
  }
  
  /**
   * 得到當(dāng)前頁, 如果為0 則開始第一頁,否則為當(dāng)前頁
   * @param page
   * @return
   */
  public int getCurPage(int page)
  {
    int currentPage = (page == 0)? 1: page;
    
    return currentPage;
  }
  
}

4.Service層接口設(shè)計(jì),定義一個(gè)PersonService接口,里面聲明了一個(gè)方法,返回一個(gè)PageBean:

public interface PersonService
{
  public PageBean getPageBean(int pageSize, int page);
}

5.Service層接口實(shí)現(xiàn)類PersonServiceImpl類,實(shí)現(xiàn)唯一的方法:

public class PersonServiceImpl implements PersonService
{
  private PersonDAO personDAO = new PersonDAOImpl();
  
  /**
   * pageSize為每頁顯示的記錄數(shù)
   * page為當(dāng)前顯示的網(wǎng)頁
   */
  @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層設(shè)計(jì),定義一個(gè)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表示當(dāng)前網(wǎng)頁
    PageBean pageBean = personService.getPageBean(5, page);
    
    HttpServletRequest request = ServletActionContext.getRequest();
    
    request.setAttribute("pageBean", pageBean);
    
    return SUCCESS;
  }
}

7.輔助類設(shè)計(jì),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("你輸入的頁數(shù)大于最大頁數(shù),頁面將跳轉(zhuǎn)到首頁!");
        
        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>&nbsp;&nbsp;
    <font size="5">共<font color="red"><s:property value="#request.pageBean.allRows"/></font>條記錄</font><br><br>
    
    <s:if test="#request.pageBean.currentPage == 1">
      首頁&nbsp;&nbsp;&nbsp;上一頁
    </s:if>
    
    <s:else>
      <a href="personAction.action">首頁</a>
      &nbsp;&nbsp;&nbsp;
      <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>
      &nbsp;&nbsp;&nbsp;
      <a href="personAction.action?page=<s:property value="#request.pageBean.totalPage"/>">尾頁</a>
    </s:if>
    
    <s:else>
      下一頁&nbsp;&nbsp;&nbsp;尾頁
    </s:else>
  
  </center><br>
  
  <center>
    
    <form action="personAction" onsubmit="return validate();">
      <font size="4">跳轉(zhuǎn)至</font>
      <input type="text" size="2" name="page">頁
      <input type="submit" value="跳轉(zhuǎn)">
    </form>
    
  </center>
  
 </body>
</html>

至此,hibernate+struts2實(shí)現(xiàn)網(wǎng)頁分頁功能代碼部分就完畢了,像hibernate與struts的配置文件就不列出來了,那些都不是重點(diǎn)!

頁面效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot jpa分庫分表項(xiàng)目實(shí)現(xiàn)過程詳解

    springboot jpa分庫分表項(xiàng)目實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了springboot jpa分庫分表項(xiàng)目實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Mybatis-Plus使用MetaObjectHandler實(shí)現(xiàn)自動(dòng)填充實(shí)體對象字段

    Mybatis-Plus使用MetaObjectHandler實(shí)現(xiàn)自動(dòng)填充實(shí)體對象字段

    在我們使用Mybatis-Plus時(shí),一些簡單的CRUD,你會(huì)發(fā)現(xiàn)好多表,許多字段是重復(fù)的,如果我們每次更新或者新增,都要手動(dòng)賦值,那么會(huì)出現(xiàn)許多不必要的重復(fù)操作,所以本文介紹了Mybatis-Plus使用MetaObjectHandler實(shí)現(xiàn)自動(dòng)填充實(shí)體對象字段,需要的朋友可以參考下
    2024-11-11
  • mybatis連接MySQL8出現(xiàn)的問題解決方法

    mybatis連接MySQL8出現(xiàn)的問題解決方法

    這篇文章主要介紹了mybatis連接MySQL8出現(xiàn)的問題解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法

    SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法

    這篇文章主要介紹了SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • SpringBoot解決跨域請求攔截問題代碼實(shí)例

    SpringBoot解決跨域請求攔截問題代碼實(shí)例

    這篇文章主要介紹了SpringBoot解決跨域請求攔截代碼實(shí)例,在微服務(wù)開發(fā)中,一個(gè)系統(tǒng)包含多個(gè)微服務(wù),會(huì)存在跨域請求的場景。 本文講解SpringBoot解決跨域請求攔截的問題。,需要的朋友可以參考下
    2019-06-06
  • Java Springboot的目的你知道嗎

    Java Springboot的目的你知道嗎

    在本篇文章中小編給大家分析了Java中Spring Boot的優(yōu)勢以及相關(guān)知識點(diǎn)內(nèi)容,興趣的朋友們可以學(xué)習(xí)參考下,希望能夠給你帶來幫助
    2021-09-09
  • java自定義動(dòng)態(tài)鏈接數(shù)據(jù)庫示例

    java自定義動(dòng)態(tài)鏈接數(shù)據(jù)庫示例

    這篇文章主要介紹了java自定義動(dòng)態(tài)鏈接數(shù)據(jù)庫示例,需要的朋友可以參考下
    2014-02-02
  • ObjectInputStream 和 ObjectOutputStream 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    ObjectInputStream 和 ObjectOutputStream 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    ObjectInputStream 和 ObjectOutputStream 的作用是,對基本數(shù)據(jù)和對象進(jìn)行序列化操作支持。本文給大家詳細(xì)介紹了ObjectInputStream 和 ObjectOutputStream的相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧
    2017-05-05
  • SpringBoot發(fā)送html郵箱驗(yàn)證碼功能

    SpringBoot發(fā)送html郵箱驗(yàn)證碼功能

    這篇文章主要介紹了SpringBoot發(fā)送html郵箱驗(yàn)證碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • 一篇文章帶你了解Java方法的使用

    一篇文章帶你了解Java方法的使用

    這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08

最新評論

兴仁县| 天等县| 饶河县| 封开县| 丹阳市| 东方市| 奎屯市| 从化市| 尖扎县| 杭锦后旗| 阜新市| 云霄县| 衡水市| 额尔古纳市| 罗源县| 长乐市| 白银市| 乌拉特后旗| 渝中区| 资兴市| 新田县| 马鞍山市| 东源县| 安仁县| 闽侯县| 泽州县| 西宁市| 营山县| 盐城市| 右玉县| 四川省| 肥东县| 合肥市| 文安县| 新野县| 健康| 五指山市| 济阳县| 永吉县| 陇西县| 东海县|