分页struts+spring+hibernate

提供交流与学习..有不对的地方请给指出,,,本人非常感谢..呵呵....

struts+spring+hibernate实现数据库记分页显示

要分页就必须要有取数据库的起始位置和取多少记录,还有要有总记录

用spring来结合hibernate实现分页,
首先创建一个PageBean.java:

package com.binghe.spring;

public class PageBean {
 private int count = 0; // 记录总数

 private int pageSize = 20; // 每页显示记录数

 private int pageCount = 0; // 总页数

 private int page = 1; // 当前页数

 private String totalCountSQL;// 得到总记录数sql语句

 private String listSQL;// 得到查询记录sql语句

 public int getCount() {
  return count;
 }
 public void setCount(int count) {
  if (pageSize != 0) {
   pageCount = count / pageSize;
   if (count % pageSize != 0) {
    pageCount++;
   }
  }
  this.count = count;
 }
 public String getListSQL() {
  return listSQL;
 }
 public void setListSQL(String listSQL) {
  this.listSQL = listSQL;
 }
 public int getPage() {
  return page;
 }
 public void setPage(int page) {
  this.page = page;
 }
 public int getPageCount() {
  return pageCount;
 }
 public void setPageCount(int pageCount) {
  this.pageCount = pageCount;
 }
 public int getPageSize() {
  return pageSize;
 }
 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }
 public String getTotalCountSQL() {
  return totalCountSQL;
 }
 public void setTotalCountSQL(String totalCountSQL) {
  this.totalCountSQL = totalCountSQL;
 }
}
第二,创建一个接口:PaginateIntece.java

package com.binghe.spring;

import java.io.Serializable;
import java.util.List;

public interface PaginateIntece extends Serializable {
 public List getList(PageBean page);

 public String getToolsMenu(PageBean page);

 public int getTotalCount(PageBean p, String str[], Object ob2[])
   throws Exception;

 public int getTotalCount(PageBean page) throws Exception;

 public List getList(PageBean page, String str[], Object ob2[])
   throws Exception;
}
第三,创建一个继承spring 的org.springframework.orm.hibernate3.support.HibernateDaoSupport的类Paginate.java,原码如下

package com.binghe.spring;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class Paginate extends HibernateDaoSupport implements PaginateIntece {

 /**
  * 显示用的菜单
  */
 public String getToolsMenu(PageBean p) {
  StringBuffer str = new StringBuffer("");
  int next, prev;
  prev = p.getPage() - 1;
  next = p.getPage() + 1;

  if (p.getPage() > 1) {
   str
     .append("}

你可能感兴趣的:(spring,sql,Hibernate,bean,struts)