数据库分页技术的实现

            在Web开发中,为了方便美观的展示数据,使用分页是必不可少的一项技术。然后实现分页的方法有多种多样,在这里我就以我自己的理解和方式给大家演示一下怎么做分页,做分页都需要哪些准备工作。

准备工作:

1.在Web编译工具中创建Web工程

2.搭建低层的运行环境。

3.开发实体类,实现逻辑代码的统一调用,完成彼此之间的分工和协作。

     我是用SpringMvc模式开发的数据库分页技术的Demo例子。在这里搭建底层环境我就不一一讲解了。不会,可以问度娘。创建完成后,工程架构如下:


在开发数据可分页一些实体之前,我们手先应该搞明白,数据之间的关系和流动去向。和需要创建的实体以及他们之间的关系。明白这些,在做这个分页就不难了。该技术流程图大致如下:


         在上述图片中我们可以看到,jsp页面需要展示的东西,然后jsp的数据是由Servlet提供,QueryInfo是前台传递过来的数据然后封装在该实体中,Serlvet中书写Service方法,Service要想拿到数据必须访问数据库,然后访问数据库后拿到了数据,将数据封装在QueryResult实体中,返回给Service,在Service方法中负责生成需要显示的jsp页面数据对象即PageBean,然后将封装在PageBean中的数据由Servlet返回给前台的jsp页面,然后用户就可以看到从数据库中读取到的数据。

搭建好了Dao、Service和配置文件以及数据库的环境。下面我们就开发这三个实体对象QueryInfo、QueryResult、PageBean.

QueryInfo实体类代码如下:

package com.nyist.cn.Pagination;


public class QueryInfo {
private int currentpage = 1; //用户当前看的页
private int pagesize = 8; //页面大小
private int startindex; //记住 用户看的页的数据在数据库的起始位置

public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getStartindex() {
this.startindex = (this.currentpage -1)*this.pagesize;
return startindex;
}

}


QueryResult实体类代码如下:

package com.nyist.cn.Pagination;


import java.util.List;


public class QueryResult {
private List list; //记住用户看的数据
private int totalrecord; //记录数
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(int totalrecord) {
this.totalrecord = totalrecord;
}

}


PageBean实体类代码如下:

package com.nyist.cn.Pagination;




import java.util.Arrays;
import java.util.List;


import com.nyist.cn.mode.person;




public class PageBean {
private List<person> list;
private int totalrecord;
private int pagesize;
private int totalpage;
private int currentpage;
private int previouspage;
private int nextpage;
private int[] pagebar = new int[]{};
public List<person> getList() {
return list;
}
public void setList(List<person> list) {
this.list = list;
}
public int getTotalrecord() {
return totalrecord;
}

public void setTotalrecord(int totalrecord) {
this.totalrecord = totalrecord;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getTotalpage() {
//100  5  20
//101  5  21
//99   5  20
if(this.totalrecord%this.pagesize==0){
this.totalpage = this.totalrecord/this.pagesize;
}else{
this.totalpage = (this.totalrecord/this.pagesize)+1;
}
    return totalpage;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getPreviouspage() {
if(currentpage-1<1){
this.previouspage=this.currentpage;
}else {
this.previouspage = this.currentpage-1;
}
return previouspage;
}
public int getNextpage() {
if(this.currentpage+1>this.totalpage){
this.nextpage = this.totalpage;
}else {
this.nextpage = this.currentpage+1;
}
return nextpage;
}
public void setTotalpage(int totalpage) {
this.totalpage = totalpage;
}
public int[] getPagebar() {
//1.第一种方式  页码条显示
/*int[] pagebar = new int[this.totalpage];
for(int i=1;i<=this.totalpage;i++){
pagebar[i-1] = i;
}*/

//2.第二种方式页码条显示    动态页码条
int pagebar[] = null;
int startpage;
int endpage;
if(this.totalpage <= 10){
pagebar = new int[this.totalpage];
startpage = 1;
endpage = this.totalpage;
}else{
pagebar = new int[10];
startpage = this.currentpage - 4;
endpage = this.currentpage + 5;
//总页数30      3    -1
//总页数30      29   34   21     30
if(startpage < 1){
startpage = 1;
endpage = 10;
}

//最后显示的页数  大于  总页码数的情况
if(endpage > this.totalpage){
endpage = this.totalpage;
startpage = this.totalpage - 9;
}
}

//将  页码条 填充数组
int index = 0;
for (int i = startpage; i <=endpage; i++) {
pagebar[index++] = i;
}

this.pagebar = pagebar;
return pagebar;
}

@Override
public String toString() {
return "PageBean [list=" + list + ", totalrecord=" + totalrecord + ", pagesize=" + pagesize + ", totalpage="
+ totalpage + ", currentpage=" + currentpage + ", previouspage=" + previouspage + ", nextpage="
+ nextpage + ", pagebar=" + Arrays.toString(pagebar) + "]";
}

}


WebUtil实体类代码如下:    WebUtil使用来将request对象里带过来的数据封装到QueryInfo中去

package com.nyist.cn.Util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;


import javax.servlet.http.HttpServletRequest;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;


public class WebUtil {

public static <T> T request2Bean(HttpServletRequest request,Class<T>  beanClass){
try{
T bean = beanClass.newInstance();
//得到request里面所有数据
Map map = request.getParameterMap();
//map{name=aa,password=bb,birthday=1990-09-09}  bean(name=aa,password=dd,birthday=Date)

ConvertUtils.register(new Converter(){


public Object convert(Class type, Object value) {
if(value==null){
return null;
}
String str = (String) value;
if(str.trim().equals("")){
return null;
}

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
BeanUtils.populate(bean, map);   
return bean;
}catch (Exception e) {
throw new RuntimeException(e);
}
}

public static String generateID(){
return UUID.randomUUID().toString();
}

}


Dao包下的Mappe接口文件中声明的方法如下:



Service接口中声明的方法如下:



Dao包下的Impl包中的Mapper接口的实现配置文件内容如下:


            比较重要的几个代码截图如上,骑他的代码省略,就是一般的SpringMvc的开发步骤。将SpringMvc的开发步骤完善一下,在将如上代码截图和如上代码理解并添加到你的代码中即可完成数据可分页。

分页效果如下:



猜你喜欢

转载自blog.csdn.net/lvhaoguang0/article/details/80877042
今日推荐