基于hibernate实现的分页技术实例分析
程序员文章站
2024-03-11 11:07:19
本文实例讲述了基于hibernate实现的分页技术。分享给大家供大家参考,具体如下:
先说明一下基于hibernate实现分页的原理,假如从数据库取出100条数据,我们要...
本文实例讲述了基于hibernate实现的分页技术。分享给大家供大家参考,具体如下:
先说明一下基于hibernate实现分页的原理,假如从数据库取出100条数据,我们要让每页显示10条,假如从30开始,只需要设置起始位置和最大的返回结果即可
先上代码:注意传进来的参数有 page这类,后面有介绍
public list<article> querybypage(final string username, final page page) { return this.gethibernatetemplate().executefind(new hibernatecallback() { public object doinhibernate(session session) throws hibernateexception, sqlexception { query query = session.createquery("select art from article art where art.username = ?"); //设置参数 query.setparameter(0, username); //设置每页显示多少个,设置多大结果。 query.setmaxresults(page.geteverypage()); //设置起点 query.setfirstresult(page.getbeginindex()); return query.list(); } });
上面关键代码是 setmaxresults(),和setfirstresult(),即设置最大显示值和起点
这里我们需要一个page工具类,用来操作分页。
page.java:
package com.fenye; public class page { // 1.每页显示数量(everypage) private int everypage; // 2.总记录数(totalcount) private int totalcount; // 3.总页数(totalpage) private int totalpage; // 4.当前页(currentpage) private int currentpage; // 5.起始点(beginindex) private int beginindex; // 6.是否有上一页(hasprepage) private boolean hasprepage; // 7.是否有下一页(hasnextpage) private boolean hasnextpage; public page(int everypage, int totalcount, int totalpage, int currentpage, int beginindex, boolean hasprepage, boolean hasnextpage) { this.everypage = everypage; this.totalcount = totalcount; this.totalpage = totalpage; this.currentpage = currentpage; this.beginindex = beginindex; this.hasprepage = hasprepage; this.hasnextpage = hasnextpage; } //构造函数,默认 public page(){} //构造方法,对所有属性进行设置 public int geteverypage() { return everypage; } public void seteverypage(int everypage) { this.everypage = everypage; } public int gettotalcount() { return totalcount; } public void settotalcount(int totalcount) { this.totalcount = totalcount; } 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 getbeginindex() { return beginindex; } public void setbeginindex(int beginindex) { this.beginindex = beginindex; } public boolean ishasprepage() { return hasprepage; } public void sethasprepage(boolean hasprepage) { this.hasprepage = hasprepage; } public boolean ishasnextpage() { return hasnextpage; } public void sethasnextpage(boolean hasnextpage) { this.hasnextpage = hasnextpage; } }
page工具类主要是封装页面信息,一共多少数据啊,一页显示多少啊,起点的序号,总页数,是否有上一页下一页,当前页。
还需要一个操作page的工具类,pageutil.java
package com.sanqing.fenye; /* * 分页信息辅助类 */ public class pageutil { public static page createpage(int everypage,int totalcount,int currentpage) { everypage = geteverypage(everypage); currentpage = getcurrentpage(currentpage); int totalpage = gettotalpage(everypage, totalcount); int beginindex = getbeginindex(everypage, currentpage); boolean hasprepage = gethasprepage(currentpage); boolean hasnextpage = gethasnextpage(totalpage, currentpage); return new page(everypage, totalcount, totalpage, currentpage, beginindex, hasprepage, hasnextpage); } public static page createpage(page page,int totalcount) { int everypage = geteverypage(page.geteverypage()); int currentpage = getcurrentpage(page.getcurrentpage()); int totalpage = gettotalpage(everypage, totalcount); int beginindex = getbeginindex(everypage, currentpage); boolean hasprepage = gethasprepage(currentpage); boolean hasnextpage = gethasnextpage(totalpage, currentpage); return new page(everypage, totalcount, totalpage, currentpage, beginindex, hasprepage, hasnextpage); } //设置每页显示记录数 public static int geteverypage(int everypage) { return everypage == 0 ? 10 : everypage; } //设置当前页 public static int getcurrentpage(int currentpage) { return currentpage == 0 ? 1 : currentpage; } //设置总页数,需要总记录数,每页显示多少 public static int gettotalpage(int everypage,int totalcount) { int totalpage = 0; if(totalcount % everypage == 0) { totalpage = totalcount / everypage; } else { totalpage = totalcount / everypage + 1; } return totalpage; } //设置起始点,需要每页显示多少,当前页 public static int getbeginindex(int everypage,int currentpage) { return (currentpage - 1) * everypage; } //设置是否有上一页,需要当前页 public static boolean gethasprepage(int currentpage) { return currentpage == 1 ? false : true; } //设置是否有下一个,需要总页数和当前页 public static boolean gethasnextpage(int totalpage, int currentpage) { return currentpage == totalpage || totalpage == 0 ? false : true; } }
创建page只需要3个参数,每页显示多少数据,当前页,总共多少数据,其他的4个参数都可以通过这三个计算出来
所以后面要创建page,只需要调用这工具方法pageutil.createpage(3个参数),就返回一page.
返回的page就是前面参数的page,即要显示的分页
这样就算完成了分页的功能。
希望本文所述对大家基于hibernate框架的java程序设计有所帮助。
上一篇: struts2.5环境搭建
下一篇: phpmailer发送邮件功能