首页 > 代码库 > hibernate实现分页

hibernate实现分页

创建以实现分页的工具类

package com.jdztc.entity;

import java.util.List;

/**
 * 这是实现分页的工具类
 * @author 付鹏鹏
 *
 */
public class PageModel {
    private  int currPage;//当前页
    private int totalRecords;//总的记录数
    private List<?> list;//结果集
    private int pageSize;//每页记录数
    private int allPage;//总的页数
    private int firstPage;//第一页
    private int beforePage;//上一页
    private int afterPage;//下一页
    private int lastPage;//
    public int getCurrPage() {
        return currPage;
    }
    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }
    public int getTotalRecords() {
        return totalRecords;
    }
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }
    public List<?> getList() {
        return list;
    }
    public void setList(List<?> list) {
        this.list = list;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getAllPage() {
        return allPage;
    }
    public void setAllPage(int allPage) {
        this.allPage = allPage;
    }
    public int getFirstPage() {
        return firstPage;
    }
    public void setFirstPage(int firstPage) {
        this.firstPage = firstPage;
    }
    public int getBeforePage() {
        return beforePage;
    }
    public void setBeforePage(int beforePage) {
        this.beforePage = beforePage;
    }
    public int getAfterPage() {
        return afterPage;
    }
    public void setAfterPage(int afterPage) {
        this.afterPage = afterPage;
    }
    public int getLastPage() {
        return lastPage;
    }
    public void setLastPage(int lastPage) {
        this.lastPage = lastPage;
    }

    public static int getAllpage1(int totalRecords, int pageSize) {
        int Allpage = (totalRecords + pageSize - 1) / pageSize;
        return Allpage;

    }

    public static int getBeforepage1(int currPage) {

        // 这样写并没有错误只是再调用时需要太多本个参数
        // 如果当前页是第一页那么

        return currPage <= 1 ? 1 : currPage - 1;
    }

    public static int getAfterpage1(int currPage, int totalRecords, int pageSize) {
        // 如果当前也为最后一页

        return currPage >= getLastpage1(totalRecords, pageSize) ? getLastpage1(totalRecords, pageSize) : currPage + 1;
    }

    public static int getLastpage1(int totalRecords, int pageSize) {
        return getAllpage1(totalRecords, pageSize) <= 0 ? 1 : getAllpage1(totalRecords, pageSize);
    }
}
/**
     *servlet中 实现分页
     */
    @Test
    public void conturepage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currpage=req.getParameter("currPage");
        System.out.println("....但钱页数"+currpage);
        //设置当前页面
        int start=1;
        //设置显示每页的记录数
        int pagesize=5;
        if(currpage!=null&&!currpage.isEmpty()){
             start=Integer.parseInt(currpage);
        }
        PageModel pagemodel=MessageDao.get(start, pagesize);
        //将分页对象发送到会话当中
        System.out.println("....总的记录啊"+pagemodel.getTotalRecords());
        System.out.println("当前页"+pagemodel.getCurrPage());
        System.out.println("下一页"+pagemodel.getAfterPage());
        System.out.println("上一页"+pagemodel.getBeforePage());
        System.out.println("最后一页"+pagemodel.getLastPage());
        System.out.println("总页数"+pagemodel.getAllPage());
        req.getSession().setAttribute("list", pagemodel.getList());
        req.getSession().setAttribute("pagemodel",pagemodel);
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }
/**
     *Dao类中的方法 实现根据留言时间来进行分页查询
     * @param pagesize 
     * @param start 
     * @param pagesize 
     * @param start 
     */
    @Test
    public static PageModel get(int start, int pagesize){
        SessionFactory sessionFactory=null;
        Session session=null;
        Transaction ts=null;
        PageModel pagemodel=null;
        try {
            sessionFactory=HibernateUtil.getSessionFactory();
            session=sessionFactory.openSession();
            ts=session.beginTransaction();
            Query query=session.createQuery("from Message m order by m.mtime desc");
            //然后再根据这个进行分页查询
            //设置开始位置
            query.setFirstResult((start-1)*pagesize);
            //设置每页显示的记录数
            query.setMaxResults(pagesize);
            List<Message> list=query.list();
            //得到总的记录数
            int totalRecord=getAllpage(session,ts);
            System.out.println(totalRecord);//6
            //得到总的页数
            int allpage=PageModel.getAllpage1(totalRecord, pagesize);
            System.out.println(allpage);//2
            //得到第一页
            //得到下一页
            int afterpage=PageModel.getAfterpage1(start, totalRecord, pagesize);
            System.out.println(afterpage);//2
            //得到上一页
            int beforepage=PageModel.getBeforepage1(start);
            System.out.println(beforepage);//1
            //得到最后一页
            int lastpage=PageModel.getLastpage1(totalRecord, pagesize);
            System.out.println(lastpage);//2
            //实例化pageModel对象来进行设置分页显示的数据
            for (Message message : list) {
                System.out.println("message  text"+message.getMtext());
            }
            pagemodel=new PageModel();
            pagemodel.setCurrPage(start);
            pagemodel.setPageSize(pagesize);
            //设置得到的集合
            pagemodel.setList(list);
            //设置总的记录数
            pagemodel.setTotalRecords(totalRecord);
            //设置上一页
            pagemodel.setBeforePage(beforepage);
            //设置下一页
            pagemodel.setAfterPage(afterpage);
            //设置最后一页
            pagemodel.setLastPage(lastpage);
            pagemodel.setAllPage(allpage);
            ts.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            ts.rollback();
        }
        return pagemodel;
        
    }

根据当前页和每页的容量 来得到下一页和上一页总页数   然后传到当前请求当中  在前段页面中进行获取下一页和上一页

 <div class="l-limit">
                   总记录数:<span id="tatal">${sessionScope.pagemodel.totalRecords}</span>
                    &nbsp 当前<span id="currpage">${sessionScope.pagemodel.currPage}</span>
                    /<span id="tatalpage">${sessionScope.pagemodel.allPage}</span>
                    &nbsp<a id="beforepage" href="MessageServlet?method=conturepage&currPage=${sessionScope.pagemodel.beforePage}" >上一页</a>
                    <a id="afterpage" href="MessageServlet?method=conturepage&currPage=${sessionScope.pagemodel.afterPage}">下一页</a>
                    <select class="limitpage" id="limit-page">
                        <option checked="selected">第一页</option>
                        <option></option>
                    </select>
            </div>

遇到的问题

Property ‘Allpage‘ not found on type com.jdztc.entity.PageModel] with root cause
原因:分页工具类中的属性Allpage方法与get()set()的方法中的get()方法重名了

hibernate实现分页