Java实现简单的分页功能
程序员文章站
2023-12-22 08:27:40
本文实例为大家分享了java实现分页功能的具体代码,供大家参考,具体内容如下
不用根据改变sql的形式去查询;
直接查询所有的数据,根据页码自动显示数据;
分页...
本文实例为大家分享了java实现分页功能的具体代码,供大家参考,具体内容如下
不用根据改变sql的形式去查询;
直接查询所有的数据,根据页码自动显示数据;
分页对象
public class pageutils implements serializable { /** * */ private static final long serialversionuid = -5247614532234782640l; public final static string page = "page"; public final static string page_no = "pageno"; public final static string page_size = "pagesize"; private long pagesize=10;//每页显示记录数 private long firstresult=0;//当页第一条记录号 private long totalcount;//总记录数 private long totalpage;//总页码 private long pageno=1;//当前页码 private list<?> sumdata;//此集合可用来保存 合计数据 private list<?> data;//查询结果 public long getpagesize() { return pagesize; } public void setpagesize(long pagesize) { this.pagesize = pagesize; } public long getfirstresult() { if(pageno>0){ firstresult=pagesize * (pageno -1); }else{ firstresult = 0; } return firstresult; } public long getnextpageresult(){ if(pageno>0){ return pagesize*(pageno-1); }else{ return pageno; } } public void setfirstresult(long firstresult) { this.firstresult = firstresult; } public long gettotalcount() { return totalcount; } public void settotalcount(long totalcount) { this.totalcount = totalcount; totalpage = this.totalcount/pagesize; if (totalpage == 0 || totalcount % pagesize != 0) { totalpage++; } } public long gettotalpage() { return totalpage; } public void settotalpage(long totalpage) { this.totalpage = totalpage; } public long getpageno() { return pageno; } public void setpageno(long pageno) { this.pageno = pageno; } public list<?> getdata() { return data; } public void setdata(list<?> data) { this.data = data; } /** * 是否第一页 */ public boolean isfirstpage() { return pageno <= 1; } /** * 是否最后一页 */ public boolean islastpage() { return pageno >= gettotalpage(); } /** * 下一页页码 */ public long getnextpage() { if (islastpage()) { return pageno; } else { return pageno + 1; } } /** * 上一页页码 */ public long getprepage() { if (isfirstpage()) { return pageno; } else { return pageno - 1; } } public pageutils(){} public pageutils(long pageno){ this.pageno=pageno; } public pageutils(long pageno,long pagesize){ this.pageno=pageno; this.pagesize = pagesize; } public list<?> getsumdata() { return sumdata; } public void setsumdata(list<?> sumdata) { this.sumdata = sumdata; } }
查询的数据实体
在查询的实体里添加页码和每页显示条数参数;
private int pagesize; //每页显示的条数 private int pageno; //当前页码 public int getpagesize() { return pagesize; } public void setpagesize(int pagesize) { this.pagesize = pagesize; }
控制层controller
@requestmapping("/list") public string list(model model,channelvalueinfoview input) { // input:传入的参数为对象 pageutils page=new pageutils(); //如果传入的当前条数为0,则赋予值(首次查询不带参); if(input.getpagesize()==0){ //当前页码第一页 input.setpageno(1); //每页显示条数,当前每页显示10条数据; input.setpagesize(10); } page.setpageno(input.getpageno()); page.setpagesize(input.getpagesize()); //核心分页代码 pagehelper p=new pagehelper(); page<channelvalueinfolist> l=p.startpage(input.getpageno(),input.getpagesize()); //紧跟着的第一个select查询将会被分页 channelvalueservice.getchannelvalueinfoviewlist(input); model.addattribute("input", input); page.setdata(l); page.settotalcount(l.gettotal()); model.addattribute("page", page); return "index"; }
页面处理
//循环穿过来的page.data数据 <tr th:each="ts : ${page.data}"> <td th:text="${ts.channelvaluename}"></td> ---------- <form id="content_form" action="/channelvalue/list" method="post" > <div> 总数:<span id="totalcount" th:text="${page.totalcount}">0</span> </div> <ul class="pagination"> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onfirst()">首页</a> </li> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onpre()"><</a> </li> <li class="active"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <span id="beginrow" th:text="${page.pageno}">0</span> </a> </li> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onnext()">></a> </li> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onlast()">尾页</a> </li> </ul> </for m> ---------- <script> function onfirst() { onlist(1); } function onpre() { var beginrow = parseint($('#beginrow').html()); if (beginrow - 1 > 0) { onlist(beginrow - 1); } } function onnext() { var beginrow = parseint($('#beginrow').html()); var totalcount = parseint($('#totalcount').html()); var pagesize = parseint($('#pagesize').val()); if (parseint(totalcount / pagesize + 1) > beginrow + 1) { onlist(beginrow+1); } } function onlast() { var totalcount = parseint($('#totalcount').html()); var pagesize = parseint($('#pagesize').val()); onlist(parseint(totalcount / pagesize + 1) - 1); } function onlist(pageno) { if (pageno == 0) pageno = 1; $('#pageno').val(pageno); $("#content_form").submit(); } </script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。