SpringMVC+Hibernate+Spring框架之一(Hibernate 分页实现,视图采用Freemarker)

来源:互联网 发布:js获取div id值 编辑:程序博客网 时间:2024/05/16 04:48

最近自己在做一个小系统,自己准备写一套小型的SpringMVC+Hibernate+Spring+Freemarker框架,首先用到了分页基础功能。

 

1、首先自己得搞一个分页的Javabean ,我就定义为Page,代码如下:

import java.util.List;

public class Page {

 // 总记录数
 private int totalSize = Constants.PAGE_TOTAL_SIZE_DEFAULT;

 // 当前页
 private int currentPageNo = Constants.PAGE_CURRENT_PAGE_DEFAULT;

 // 每页的记录数
 private int pageSize = Constants.PAGE_RECORED_SIZE_DEFAULT;

 // 总的页数
 private int totalPageCount;

 // 返回的数据记录
 private List<?> records;

 public Page() {
 }

 public int getTotalSize() {
  return totalSize;
 }

 public void setTotalSize(int totalSize) {
  this.totalSize = totalSize;
 }

 public int getCurrentPageNo() {
  return currentPageNo;
 }

 public void setCurrentPageNo(int currentPageNo) {
  this.currentPageNo = currentPageNo;
 }

 public int getPageSize() {
  return pageSize;
 }

 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }

 public List<?> getRecords() {
  return records;
 }

 public void setRecords(List<?> records) {
  this.records = records;
 }

 public int getTotalPageCount() {
  if (totalSize % pageSize == 0) {
   totalPageCount = totalSize / pageSize;
  } else {
   totalPageCount = totalSize / pageSize + 1;
  }
  return totalPageCount;
 }

 public void setTotalPageCount(int totalPageCount) {
  this.totalPageCount = totalPageCount;
 }

 /**
  * 是否有下一页
  *
  * @return
  */
 public boolean hasNextPage() {
  return currentPageNo < totalPageCount;
 }

 /**
  * 是否有前一页
  *
  * @return
  */
 public boolean hasPreviousPage() {
  return currentPageNo > Constants.PAGE_CURRENT_PAGE_DEFAULT;
 }

 /**
  * 获取当前页的第一条记录的索引位置
  * @return
  */
 public int getStartIndexOfCurrentPage() {
  return (currentPageNo - 1) * pageSize;
 }
 
}

 

2、Dao层hibernate代码,简单,同时把结果存储在Page中了,代码如下:

 

@SuppressWarnings("unchecked")
 @Override
 public Page findAll(Page page) {
  Session session = sessionFactory.getCurrentSession();
  
  int totalSize = session.createCriteria(entityClass).list().size();
  page.setTotalSize(totalSize);
  
  List<T> list = session.createCriteria(entityClass)
     .setFirstResult(page.getStartIndexOfCurrentPage())
     .setMaxResults(page.getPageSize()).list();
  page.setRecords(list);
  return page;
 }

 

3、视图层实现采用Freemarker写的,代码和jsp查不多。由于使用按钮显示所以跳转是onclick,在js代码写的。

<script type="text/javascript">
   function firstPage(){
    window.location.href='accounts?currentPageNo=1';
   }
   
   function nextPage(){
    window.location.href='accounts?currentPageNo=${page.currentPageNo+1}';
   }
   
   function previousPage(){
    window.location.href='accounts?currentPageNo=${page.currentPageNo-1}';
   }
   
   function lastPage(){
    window.location.href='accounts?currentPageNo=${page.totalPageCount}';
   }
   
   function goPage(){
    var currentPageNo = document.getElementById("currentPageNo").value;
    window.location.href="accounts?currentPageNo="+currentPageNo;
   }
  </script>
  <button title="第一页" onclick="firstPage()">第一页</button>&nbsp;
  <button onclick="previousPage()" <#if !page.hasPreviousPage()>disabled="disabled"</#if>>前一页</button>&nbsp;
  <button onclick="nextPage()" <#if !page.hasNextPage()>disabled="disabled"</#if>>下一页</button>&nbsp;
  <button onclick="lastPage()">最后一页</button>&nbsp;
  <input type="text" name="currentPageNo" id="currentPageNo" size="2" value="${page.currentPageNo }">
  <button onclick="goPage()">GO</button>
  

最后,赞一下自己哈!

 

 

 

0 0
原创粉丝点击