分页插件开发(3)--后台框架搭建

来源:互联网 发布:淘宝购买叶罗丽娃娃 编辑:程序博客网 时间:2024/05/01 06:56

前面两篇文章讲的都是前端的实现,本篇也是最后一篇主要讲后台框架的搭建。

第一篇:分页插件开发(1)--jstl自定义标签 

第二篇: 分页插件开发(2)--页面按钮显示 


后台框架使用的是spring MVC+mybatis+spring,这里不会讲ssm的整合,只是贴出几个核心实现类的代码。


PageList.java

package com.lxg.util;import java.io.Serializable;import java.util.ArrayList;import java.util.List;public class PageList<T> implements Serializable {public PageList() {}public PageList(PageProperty pp, int allCount, List<T> list) {if(pp.getNpage()>0){this.page = pp.getNpage();}if(pp.getNpagesize()>0){this.pageSize = pp.getNpagesize();}this.totalRecords = allCount;if (totalRecords % pageSize > 0) {this.totalPages = totalRecords / pageSize + 1;} else {this.totalPages = totalRecords / pageSize;}this.setRecords(list);}public PageList(int page,int pageSize, int allCount, List<T> list) {if(page>0){this.page = page;}if(pageSize>0){this.pageSize = pageSize;}this.totalRecords = allCount;if (totalRecords % pageSize > 0) {this.totalPages = totalRecords / pageSize + 1;} else {this.totalPages = totalRecords / pageSize;}this.setRecords(list);}private int page = 1;private int totalRecords;private int totalPages;private int pageSize = 20;private int numbersPerBlock = 10;private List<T> records = new ArrayList<T>();public List<T> getRecords() {return records;}public void setRecords(List<T> records) {this.records = records;}public int getPage() {return page;}public void setPage(int page) {if (page < 1)page = 1;this.page = page;}public int getPageNumber() {int pageNumber = 0;if (totalRecords % pageSize == 0)pageNumber = totalRecords / pageSize;elsepageNumber = 1 + totalRecords / pageSize;return pageNumber;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getTotalRecords() {return totalRecords;}public void setTotalRecords(int totalRecords) {this.totalRecords = totalRecords;}/** * first row count of current page, start from 1 *  * @return */public int getFirstRow() {return (page - 1) * pageSize + 1;}/** * last row count of current page *  * @return */public int getLastRow() {return page == getPageNumber() ? getTotalRecords() : page * pageSize;}public int getPreviousPage() {return page > 1 ? page - 1 : page;}public int getNextPage() {return page < getPageNumber() ? page + 1 : page;}public int getBlocks() {if (this.getPageNumber() % this.numbersPerBlock == 0) {return this.getPageNumber() / this.numbersPerBlock;} else {return 1 + this.getPageNumber() / this.numbersPerBlock;}}public int getBlock() {if (this.getPage() % this.numbersPerBlock == 0) {return this.getPage() / this.numbersPerBlock;} else {return 1 + this.getPage() / this.numbersPerBlock;}}public int getNumbersPerBlock() {return numbersPerBlock;}public void setNumbersPerBlock(int numberPerBlock) {this.numbersPerBlock = numberPerBlock;}public int getPageOfPrevBlock() {if (this.getBlock() > 1) {return (this.getBlock() - 1) * this.getNumbersPerBlock();} else {return 1;}}public int getPageOfNextBlock() {if (this.getBlock() < this.getBlocks()) {return this.getBlock() * this.getNumbersPerBlock() + 1;} else {return this.getTotalRecords();}}public int getTotalPages() {return totalPages;}public void setTotalPages(int totalPages) {this.totalPages = totalPages;}}

PageProperty.java

package com.lxg.util;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class PageProperty implements Serializable {private static final long serialVersionUID = -6960121950113293333L;    private int npage;//页码private int nfirstindex;//查询起点private int npagesize;//查询数量,0表示全部private String searchString;//查询条件 where field=?private String orderString;//排序条件 order by id descprivate List parameterList;// 查询条件参数列表private HashMap<String,Object> paramMap; //参数mappublic PageProperty(int page, int pagesize,String searchString, String orderString){this.npagesize = pagesize;this.npage = page;this.searchString = searchString;this.orderString = orderString;}public PageProperty(){    this.npage=1;this.npagesize = 10;this.nfirstindex = 0;this.searchString = "";this.orderString = "";}public int getNfirstindex() {    nfirstindex=(npage-1)*npagesize;return nfirstindex;}public void setNfirstindex(int nfirstindex) {this.nfirstindex = nfirstindex;}public int getNpagesize() {return npagesize;}public void setNpagesize(int npagesize) {this.npagesize = npagesize;}public String getOrderString() {return orderString;}public void setOrderString(String orderString) {this.orderString = orderString;}public String getSearchString() {return searchString;}public void setSearchString(String searchString) {this.searchString = searchString;}public List getParameterList() {return parameterList;}public void setParameterList(List parameterList) {this.parameterList = parameterList;}public void addParamter(Object o){//增加参数initParameterList();parameterList.add(o);}public void addParamter(int index,Object o){ //增加参数initParameterList();parameterList.add(index,o);}public void clearParamter(){ //增加参数initParameterList();parameterList.clear();}public void initParameterList(){//初始化参数列表if(parameterList==null){parameterList=new ArrayList();}}public int getNpage() {    return npage;}public void setNpage(int page) {    npage = page;}public HashMap<String,Object> getParamMap() {initParamMap();return paramMap;}public void putParamMap(String name,Object o){ //增加参数initParamMap();paramMap.put(name,o);}public void clearParamMap(){ //增加参数initParamMap();paramMap.clear();}public void initParamMap(){//初始化参数列表if(paramMap==null){paramMap=new HashMap();}}public int getPageStart(){return (npage - 1)*npagesize;}public int getPageEnd(){return npage*npagesize;}}

PageUtil.java

package com.lxg.util;/** * 分页工具类 */public class PageUtil {/** * 分页中每页最大记录数 */// public static final int PAGE_SIZE = 15;/** * 根据当前页面序号得到查询数据的起始记录位 *  * @param pageNo当前页面数 */public static int getStart(int pageNo, int count, int pageSize) {if ((pageNo - 1) * pageSize >= count) {if (count % pageSize > 0) {pageNo = count / pageSize + 1;} else {pageNo = count / pageSize;}}if (pageNo < 1) {pageNo = 1;}return (pageNo - 1) * pageSize;}/** * 根据当前页面序号得到查询数据的起始记录位 *  * @param pageNo当前页面数 */public static int getStart(int pageNo, int pageSize) {if (pageNo < 1) {pageNo = 1;}return (pageNo - 1) * pageSize;}/** * 根据当前页面序号和记录总数得到查询数据的结束的最大记录位 *  * @param pageNo当前页面数 * @param count记录总数 */public static int getEnd(int pageNo, int count, int pageSize) {if (pageNo < 1) {pageNo = 1;}if (count < 0) {count = 0;}int end = 0;if (count - (pageNo - 1) * pageSize >= pageSize) {end = getStart(pageNo, pageSize) + pageSize;} else {end = count;}return end;}}

项目源码:https://github.com/li5454yong/MyTagLib.git

1 0