Hibernate分页bean

package mrmwq;

// 导入相关包文件
import java.util.List;

// 导入hibernate2资源包文件
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;

public class PaginationInfo {

    private Session session; // session对象
    private String hql = "";
    private int totalCount = 0;
    private int pageCount = 3; // 每页的显示的数据项
    private boolean hasPreviousPage = false; // 上一页
    private boolean hasNextPage = false; // 下一页
    private int totalPageNum = 1;
    private int currentPageNum = 1;

    // 私有的构造函数
    public PaginationInfo() {
    }

    // 初始化分页信息
    public void init(Session session, String hql, int pageCount) throws Exception {
        this.session = session;
        this.hql = hql;
        this.pageCount = pageCount;
        totalCount = ((Integer) session.createQuery("select count(*)" + hql).
                      uniqueResult()).intValue();
        if (this.totalCount % this.pageCount == 0) {
            this.totalPageNum = this.totalCount / this.pageCount;
        } else {
            this.totalPageNum = this.totalCount / this.pageCount + 1;
        }
        if (this.totalPageNum > 1) {
            this.hasNextPage = true;
        }
    }

    // 获得前一页信息
    public List getPreviousPage(Session session) throws HibernateException {
        this.session = session;
        if (this.getCurrentPageNum() > 1) {
            this.currentPageNum = this.currentPageNum - 1;
            if (this.currentPageNum > 1) {
                this.hasPreviousPage = true;
                this.hasNextPage = true;
            } else {
                this.hasPreviousPage = false;
                this.hasNextPage = true;
            }
        }
        return this.getPageMessage();
    }

    // 获得当前页信息
    public List getCurrentPage(Session session) throws Exception {
        this.session = session;
        return this.getPageMessage();
    }

    // 获得下一页信息
    public List getNextPage(Session session) throws Exception {
        this.session = session;
        if (this.getCurrentPageNum() < this.totalPageNum) {
            this.currentPageNum = this.currentPageNum + 1;
            if (this.currentPageNum < this.totalPageNum) {
                this.hasPreviousPage = true;
                this.hasNextPage = true;
            } else {
                this.hasPreviousPage = true;
                this.hasNextPage = false;
            }
        }
        return this.getPageMessage();
    }

    // 获得指定页信息
    public List getAppointPage(Session session, int currentPageNum) throws HibernateException {
        this.session = session;
        if (currentPageNum > 0 & currentPageNum <= this.totalPageNum) {
            this.currentPageNum = currentPageNum;
            if (this.currentPageNum > 1) {
                this.hasPreviousPage = true;
            } else {
                this.hasPreviousPage = false;
            }
            if (this.currentPageNum < this.totalPageNum) {
                this.hasNextPage = true;
            } else {
                this.hasNextPage = false;
            }
        }
        return this.getPageMessage();
    }

    // 检索指定信息的方法
    private List getPageMessage() throws HibernateException {
        Query q = session.createQuery(hql);
        q.setFirstResult((this.currentPageNum - 1) * this.pageCount);
        q.setMaxResults(this.pageCount);
        this.print();
        return q.list();
    }

    // 测试方法
    public void print() {
        System.out.println("------当前是第:" + this.getCurrentPageNum() + "/" +
                           this.getTotalPageNum() + "页");
        System.out.print("      HasPreviousPage:" + this.isHasPreviousPage());
        System.out.print("      HasNextPage:" + this.isHasNextPage());
    }

    // 返回参数值的一些方法
    public int getTotalCount() {
        return totalCount;
    }

    public int getPageCount() {
        return pageCount;
    }

    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public int getTotalPageNum() {
        return totalPageNum;
    }

    public int getCurrentPageNum() {
        return currentPageNum;
    }
}

你可能感兴趣的:(bean,Hibernate,.net)