利用Hibernate和ognl实现分页工具类

之前一直用传统的jsp实现分页,非常麻烦,还要多加一个页面用forward传值,自己看着也非常乱。学完struts和hibernate后,利用hibernate的分页功能和OGNL自己写了个分页的工具类,简单易懂,可直接使用。

  • 首先有一个Hibernate的工具类:
package com.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

final public class MySessionFactory {
    private static SessionFactory sessionFactory=null;
    private MySessionFactory(){
    }
    //单例
    static{
        sessionFactory=new Configuration().configure().buildSessionFactory();
    }
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}
  • 第二个Paging类,用来获取数据库的数据总数和每一页的数据
package com.utils;

import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;

public class Paging {
    public static int getAllRowCount(String hql) {
        Session session = MySessionFactory.getSessionFactory().openSession();
        session.beginTransaction();
        int allRows = 0;
        Query query = session.createQuery(hql);
        allRows = query.list().size();
        if (session != null && session.isOpen()) {
            session.close();
        }
        return allRows;
    }

    public static List queryByPage(String hql, int offset, int pageSize){
        Session session = MySessionFactory.getSessionFactory().openSession();
        session.beginTransaction();
        List list = null;
        Query query = session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize);
        list = query.list();
        if (session != null && session.isOpen()) {
            session.close();
        }
        return list;
    }
}
  • 第三个PageBean,包含整个Page的所有信息
package com.utils;

import java.util.List;

public class PageBean {
    private List list; //通过hql从数据库分页查询出来的list集合
    private int allRows; //总记录数
    private int totalPage; //总页数
    private int currentPage; //当前页

    private PageBean() {

    }

    public List getList() {
        return list;
    }

    public void setList(List 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;
    }

    public int getTotalPages(int pageSize, int allRows) {
        int totalPage = (allRows % pageSize == 0) ? (allRows / pageSize) : (allRows / pageSize) + 1;
        if(allRows==0) totalPage = 1;
        return totalPage;
    }

    public int getCurrentPageOffset(int pageSize, int currentPage) {
        int offset = pageSize * (currentPage - 1);
        return offset;
    }

    public int getCurPage(int page) {
        int currentPage = (page == 0) ? 1 : page;
        return currentPage;
    }

    public static PageBean getPageBean(int pageSize, int page, String hql) {
        PageBean pageBean = new PageBean();
        int allRows = Paging.getAllRowCount(hql);
        int totalPage = pageBean.getTotalPages(pageSize, allRows);
        int currentPage = pageBean.getCurPage(page);
        int offset = pageBean.getCurrentPageOffset(pageSize, currentPage);
        List list = Paging.queryByPage(hql, offset, pageSize);
        pageBean.setList(list);
        pageBean.setAllRows(allRows);
        pageBean.setCurrentPage(currentPage);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }
}
  • 当然还要有一个model类,里有三个属性。
package com.test;

public class Person {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
  • 利用action调用之前写好的PageBean,放到request里,供前台用OGNL调用。
package com.test;

import com.utils.PageBean;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

public class TestAction {
    int page;
    public String excute(){
        String hql = "from Post";
        PageBean pageBean = PageBean.getPageBean(3,page,hql);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("pageBean", pageBean);
        return "ok";
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }
}
  • 前台页面,利用#request.pageBean.list获取当前页面的数据List.测试代码如下:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path=request.getContextPath();
%>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>分页测试</h1>
    <table border="1" align="center" bordercolor="blue" width="50%">
        <s:iterator value="#request.pageBean.list" id="person">
            <tr>
                <td><s:property value="#person.id"/></td>
                <td><s:property value="#person.name"/></td>
                <td><s:property value="#person.age"/></td>
            </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="<%=path%>/test_execute.action">首页</a>
            &nbsp;&nbsp;&nbsp;
            <a href="<%=path%>/test_execute.action?page=<s:property value="#request.pageBean.currentPage - 1"/>">上一页</a>
        </s:else>

        <s:property value="#request.pageBean.currentPage"/>

        <s:if test="#request.pageBean.currentPage != #request.pageBean.totalPage">
            <a href="<%=path%>/test_execute.action?page=<s:property value="#request.pageBean.currentPage + 1"/>">下一页</a>
            &nbsp;&nbsp;&nbsp;
            <a href="<%=path%>/test_execute.action?page=<s:property value="#request.pageBean.totalPage"/>">尾页</a>
        </s:if>

        <s:else>
            下一页&nbsp;&nbsp;&nbsp;尾页
        </s:else>

        (共<s:property value="#request.pageBean.totalPage"/>页)

    </center><br>

</body>
</html>
Comments
Write a Comment