springmvc 分页查询的简单实现示例代码

我们在开发项目中很多项目都用到列表分页功能,本篇介绍了springmvc 分页查询的简单实现示例代码,非常具有实用价值,需要的朋友可以参考下。

目前较常用的分页实现办法有两种:

1.每次翻页修改sql,向sql传入相关参数去数据库实时查出该页的数据并显示

2.查出数据库某张表的全部数据,再通过在业务逻辑里面进行处理去取得某些数据并显示

对于数据量并不大的简单的管理系统而言,第一种实现方法相对来说容易使用较少的代码实现分页这一功能,本文也正是为大家介绍这种方法:

代码片段:

1,Page.java

package com.cm.contract.common; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; /**分页类 * @author FENGWEI * @date 2016-5-23 */ public class Page implements java.io.Serializable{ private static final long serialVersionUID = 1L; //前一页 private Boolean hasPrePage; //后一页 private Boolean hasNextPage; //每页显示多少条:认20条 private Long everyPage = 20L; //总页数 private Long totalPage; //当前第多少页:认第1页 private Long currentPage = 1L; //开始下标 private Long beginIndex; //结束下标 private Long endinIndex; //总共多少条 private Long totalCount; //排序列名 private String sortName; //排序状态 private String sortState; //排序信息 private String sortInfo; //是否排序 private Boolean sort = false; private String defaultInfo = " "; public String getDefaultInfo() { return defaultInfo; } public void setDefaultInfo(String defaultInfo) { this.defaultInfo = defaultInfo; } public String getSortInfo() { return sortInfo; } public void setSortInfo(String sortInfo) { this.sortInfo = sortInfo; } public String getSortName() { return sortName; } public void setSortName(String sortName) { setPageSortState(sortName); } public String getSortState() { return sortState; } public void setSortState(String sortState) { this.sortState = sortState; } public Page() { } /** * 常用,用于计算分页 * */ public Page(Long totalRecords){ this.totalCount = totalRecords; setTotalPage(getTotalPage(totalRecords)); } /** * 设置每页显示多少条时使用 * */ public Page(Long everyPage,Long totalRecords){ this.everyPage = everyPage; this.totalCount = totalRecords; setTotalPage(getTotalPage(totalRecords)); } /** * @param state 状态码 * @param value 到第多少页或者设置每页显示多少条或者为排序列名 */ public void pageState(int index,String value) { sort = false; switch (index) { case 0 :setEveryPage(Long.parseLong(value));break; case 1 :first();break; case 2: prevIoUs();break; case 3: next();break; case 4: last();break; case 5: sort = true;sort(value);break; case 6 ://到指定第多少页 setCurrentPage(Long.parseLong(value)); break; } } /** * 最前一页 */ private void first() { currentPage = 1L; } private void prevIoUs() { currentPage--; } private void next() { currentPage++; } private void last() { currentPage = totalPage; } private void sort(String sortName) { //设置排序状态 setPageSortState(sortName); } /** * 计算总页数 * */ private Long getTotalPage(Long totalRecords) { Long totalPage = 0L; everyPage = everyPage == null ? 10L : everyPage; if (totalRecords % everyPage == 0) totalPage = totalRecords / everyPage; else { totalPage = totalRecords / everyPage + 1; } return totalPage; } public Long getBeginIndex() { this.beginIndex = (currentPage - 1) * everyPage; return this.beginIndex; } public void setBeginIndex(Long beginIndex) { this.beginIndex = beginIndex; } public Long getCurrentPage() { this.currentPage = currentPage == 0 ? 1 : currentPage; return this.currentPage; } public void setCurrentPage(Long currentPage) { if(0 == currentPage){ currentPage = 1L; } this.currentPage = currentPage; } public Long getEveryPage() { this.everyPage = everyPage == 0 ? 10 : everyPage; return this.everyPage; } public void setEveryPage(Long everyPage) { this.everyPage = everyPage; } public Boolean getHasNextPage() { this.hasNextPage = (currentPage != totalPage) && (totalPage != 0); return this.hasNextPage; } public void setHasNextPage(Boolean hasNextPage) { this.hasNextPage = hasNextPage; } public Boolean getHasPrePage() { this.hasPrePage = currentPage != 1; return this.hasPrePage; } public void setHasPrePage(Boolean hasPrePage) { this.hasPrePage = hasPrePage; } public Long getTotalPage() { return this.totalPage; } public void setTotalPage(Long totalPage) { if(this.currentPage > totalPage){ this.currentPage = totalPage; } this.totalPage = totalPage; } public Long getTotalCount() { return this.totalCount; } public void setTotalCount(Long totalCount) { setTotalPage(getTotalPage(totalCount)); this.totalCount = totalCount; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } /** * 设置排序状态 * */ private void setPageSortState(String newPageSortName){ //判断之前的排序字段是否为空 if(StringUtils.isEmpty(sortName)){ //认排序为升序 this.sortState = PageUtil.ASC; this.sortInfo = PageUtil.PAGE_ASC; }else{ if(StringUtils.equalsIgnoreCase(newPageSortName, sortName)){ //判断sortState排序状态值 if(StringUtils.equalsIgnoreCase(sortState, PageUtil.ASC)){ this.sortState = PageUtil.DESC; this.sortInfo = PageUtil.PAGE_DESC; }else{ this.sortState = PageUtil.ASC; this.sortInfo = PageUtil.PAGE_ASC; } }else{ //认 this.sortState = PageUtil.ASC; this.sortInfo = PageUtil.PAGE_ASC; } } sortName = newPageSortName.toLowerCase(); } public Boolean isSort() { return sort; } public void setSort(Boolean sort) { this.sort = sort; } public Long getEndinIndex() { this.endinIndex = (currentPage) * everyPage; return endinIndex; } public void setEndinIndex(Long endinIndex) { this.endinIndex = endinIndex; } }

2.PageState.java

package com.cm.contract.common; import org.apache.commons.lang.StringUtils; /**分页状态类 * @author FENGWEI * @date 2016-5-23 */ public enum PageState { /** * 设置每页显示多少条 * */ SETPAGE, /** * 首页 * */ FirsT, /** * 向前一页 * */ PREVIoUS, /** * 向后一页 * */ NEXT, /** * 末页 * */ LAST, /** * 排序 * */ SORT, /** * 到第多少页 * */ GOPAGE; /** * @param value 索引名称 * @return 返回索引下标 */ public static int getordinal(String value) { int index = -1; if (StringUtils.isEmpty(value)) { return index; } String newValue = StringUtils.trim(value).toupperCase(); try { index = valueOf(newValue).ordinal(); } catch (IllegalArgumentException e) {} return index; } }

3.PageUtil.java

/** * 分页工具类 * @author FENGWEI * @date 2016-5-23 */ public class PageUtil { public static final String ASC = "asc"; public static final String DESC = "desc"; public static final String PAGE_DESC = "↓"; public static final String PAGE_ASC = "↑"; public static final String PAGE_NULL = " "; public static final String SESSION_PAGE_KEY = "page"; /** * 初始化分页类 * @param initPagesql分页查询sql * @param totalCount 总行数 * @param index 分页状态 * @param value 只有在设置每页显示多少条时,值不会NULL,其它为NULL */ public static Page inintPage(Long totalCount,Integer index,String value,Page sessionPage){ Page page = null; if(index

4.DefaultController.java  此部分可以灵活使用

package com.cm.contract.common; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.bind.annotation.modelattribute; /** * 提取公用的request和response Title:DefaultController Descrption: * * @author FENGWEI * @date 2016-5-6下午3:30:32 */ public class DefaultController { /** * oracel的三层分页语句 子类在展现数据前,进行分页计算! * * @param querysql * 查询sql语句,未进行分页 * @param totalCount * 根据查询sql获取的总条数 * @param columnNameDescOrAsc * 列名+排序方式 : ID DESC or ASC */ protected Page executePage(HttpServletRequest request, Long totalCount) { if (null == totalCount) { totalCount = 0L; } /** 页面状态,这个状态是分页自带的,与业务无关 */ String pageAction = request.getParameter("pageAction"); String value = request.getParameter("pageKey"); /** 获取下标判断分页状态 */ int index = PageState.getordinal(pageAction); Page page = null; /** * index

使用方法

5,Controller.java

/** * model 添加分页条件 * executePage 方法写在工具类中 * @param model */ @Controller public class CMLogController extends DefaultController { @RequestMapping("index.do") public ModelAndView userInto(ModelMap model, String username) { nameStr = username; model.addAttribute("username", nameStr); // 分页数 Long totalCount = logService.pageCounts(model); // 分页显示 Page page = executePage(request, totalCount); if (page.isSort()) { model.put("orderName", page.getSortName()); model.put("descAsc", page.getSortState()); } else { model.put("orderName", "logtime"); model.put("descAsc", "desc"); } model.put("startIndex", page.getBeginIndex()); model.put("endindex", page.getEndinIndex()); ModelAndView mv = new ModelAndView(); // 分页查询 logList = logService.Pagelist(model); mv.addobject("logList", logList); mv.setViewName("/jsp/log"); return mv; }}

6.maybatis中几条查询语句

//分页查询 select ttt.* from(select tt.*,rownum rn from(select * from CM_LOG and username like '%${username}%' AND TYPE = #{type,jdbcType=VARCHAR} order by ${orderName} ${descAsc} )tt)ttt rn > ${startIndex} // 分页数 select count(*) from CM_LOG and username like '%${username}%'

7.前台页面index.jsp

//只需在页面布局添加该div //username 为条件 // 不带条件的方式 问号必须存在

8,引用的Page.jsp

${urlParams } 共${page.totalCount}条记录 共${page.totalPage}页 每页显示${page.everyPage}条 当前第${page.currentPage}页 || 下页> 尾页>> 下一页> 末页>> 第${index}页 每页显示: ${pageCount}条

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...