最近写了一个分页,有一点心得,觉得需要记下来,毕竟好记性不如烂笔头。
本文采用的是spring+springmvc+mybatis(ssm),最近这个比较流行。具体的框架搭建这里不再叙述,相信都是有一定基础的码农。
mybatis配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins >
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
自定义page类
分页需要自定义一个page类,这个网上很多,不一定和我的一样,大致有下面几个属性
import java.util.List;
public class PagePojo {
//当前页(比如第2页,第3页)
private Integer nowPage;
//总页数
private Integer pageCount;
private Long total;
private List<?> list;
public Integer getNowPage() {
return nowPage;
}
public void setNowPage(Integer nowPage) {
this.nowPage = nowPage;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}
分页实现步骤
- 简单的sql语句
<select id="getAllTemplate" resultMap="template">
select * from template
</select>
- dao层代码
public List<Template> getAllTemplate();
- service层代码
接口层
public PagePojo getAllTemplate(Integer page,Integer pageSize);
实现层
@Override
public PagePojo getAllTemplate(Integer pageNum, Integer pageSize) {
//调用分页插件pagehepler的startPage方法,
//pageNum:当前页,(比如第2页,第3页)
//pageSize每页显示的条数
PageHelper.startPage(pageNum, pageSize);
//数据库查询出来的数据
List<Template> templates=iTemplateDao.getAllTemplate();
//将数据放入pageInfo,pageInfo会对数据进行处理,这个是封装好的类,直接调用即可
PageInfo<Template> pageInfo=new PageInfo<Template>(templates);
//将分页信息放入我们自己的pagepojo里
PagePojo page=new PagePojo();
page.setNowPage(pageInfo.getPageNum());
page.setPageCount(pageInfo.getPages());
page.setTotal(pageInfo.getTotal());
page.setList(templates);
return page;
}
- controller层代码
//为forPage,pageSize设置默认值
//forPa当前页
//pageSize每页页显示的条数
@RequestMapping("listTemplate")
public ModelAndView getAllTemplate(@RequestParam(value="forPage",defaultValue="1") Integer forPage,
@RequestParam(value="pageSize",defaultValue="5") Integer pageSize){
ModelAndView mv=new ModelAndView("listTemplate");
PagePojo page=iTemplateService.getAllTemplate(forPage, pageSize);
//返回页面一个集合
mv.addObject("pageTemplate", page);
return mv;
}
- jsp页面,解释一下关键的几行代码
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<style type="text/css">
<!--
.unnamed1 {
font-size: 18px;
line-height: normal;
}
td {
font-size: 14px;
}
a:visited {
font-size: 14px;
color: #0000FF;
text-decoration: none;
}
a:link {
font-size: 14px;
color: #0000FF;
text-decoration: none;
}
a:hover {
font-size: 14px;
color: #FF0000;
text-decoration: none;
}
-->
</style>
<table width="800" border="0" align="center">
<tr align="center">
<td colspan="2"><h2>模版列表</h2></td>
</tr>
<tr align="center" height="35" bgcolor="#ff0cc0">
<td width="80">模板编号</td>
<td width="100">模板名称</td>
<td width="100">节点个数</td>
<td width="120">创建时间</td>
<td width="100">创建人</td>
<td width="200" colspan="2">操作</td>
</tr>
<!-- 这里采用jstl提供的forEach标签对集合进行遍历,有什么不懂的百度一下forEach,不再叙述 -->
<c:forEach var='ar' items='${pageTemplate.list}' varStatus="st">
<c:choose>
<!-- 奇偶行显示的颜色不一样 -->
<c:when test="${st.count % 2 == 0}">
<tr bgcolor="white" height="30">
</c:when>
<c:otherwise>
<tr bgcolor="#cccccc" height="30">
</c:otherwise>
</c:choose>
<td>
<c:out value="${ar.templateid}"></c:out>
<!-- <s:property value="#template.templateid" id="instanceid"/> -->
</td>
<td>
<c:out value="${ar.name}"></c:out>
</td>
<td>
<c:out value="${ar.totol}"></c:out>
</td>
<td>
<c:out value="${ar.creattime}"></c:out>
</td>
<td>
<c:out value="${ar.creator}"></c:out>
</td>
<td>
<a href="../queryInstance?templateid=${ar.templateid}">查看其实例</a>
<a href="../template/modifyTemplate?templateid=${ar.templateid}">修改</a>
<a href="../template/deleteTemplate?templateId=${ar.templateid}" onclick="return confirm('确实要删除吗?')" >删除</a>
</td>
<td>
<a href="../generateInstance?templateid=${ar.templateid}">生成流程实例</a>
</td>
</c:forEach>
<tr bgcolor="#ff0cc0">
<td width="80"> </td>
<td width="100"> </td>
<td width="100"> </td>
<td width="100"> </td>
<td width="100"> </td>
<td colspan="2" width="100"> </td>
</tr>
<tr align="center">
<td height="50" colspan="5"></td>
</tr>
</table>
<!--上面都好说,这里是关键,给大家解读一下 -->
<table align="center">
<tr align="center">
<td height="50">
<!-- 点击上一页,会调用forpage(-1)这个函数,在下面有解释,在这里要多说的是,当我们每点击上一页或者下一页的时候,真正发生的事情是我们又请求了一次我们的controller,根据我们传入的参数,读取出第2页或者第三页的数据。当然,还有几种机制是全部读取出来,放入缓存里,由js控制输出,那个对数据库的压力会小很多,还有就是那个我并不会,谁会的说一声 -->
<a href="javascript:forpage(-1)">上一页</a>
<font>当前页 :${pageTemplate.nowPage}</font>
<a href="javascript:forpage(1)">下一页</a>
<font>共${pageTemplate.pageCount}页</font>
<!--这来两个input不会在页面显示,是给下面的js函数用的 -->
<input type="hidden" id="hnum" value="${pageTemplate.nowPage}" />
<input type="hidden" id="count" value="${pageTemplate.pageCount}"/>
<!--直接选择第几页,也是会调用我们的controller -->
<select name="forPage" id="forPage" onchange="changePage()" ><option >-选择页-</option>
<c:if test="${pageTemplate.pageCount>0}">
<c:set var="i" value="${1}" scope="page"/>
<c:forEach begin="1" end="${pageTemplate.pageCount}">
<option value="<c:out value="${i}"/>">
<c:out value="${i}"/>
</option>
<c:set var="i" scope="page" value="${i+1}"></c:set>
</c:forEach>
</c:if>
</select>
</td>
</tr>
</table>
<script>
function changePage(){
var forpage=document.getElementById("forPage").value;
<!--这个就是用来从新请求url的 -->
window.location.href='../template/listTemplate?forPage='+forpage;
}
var num;
var pagesize=15;
function forpage(v){
<!--获取上面隐藏的输入框的值 -->
var hnum=document.getElementById("hnum").value;
var count=document.getElementById("count").value;
var t=v;
var nn;
if(t<0){
nn=parseInt(hnum)-1;
if(nn<1){
nn=1;
window.location.href = "../template/listTemplate?forPage="+nn
}else{
window.location.href = "../template/listTemplate?forPage="+nn;
}
}
if(t>0){
nn=parseInt(hnum)+1;
if(nn>parseInt(count)){
nn=count;
window.location.href = "../template/listTemplate?forPage="+nn;
}else{
window.location.href = "../template/listTemplate?forPage="+nn;
}
}
}
function jumping() {
document.forms[0].submit();
}
function gotoPage(pageNum) {
document.URL = "?curNo=" + pageNum;
}
</script>
- 输出结果
- 福利来了
也许你们你们觉得我写的会很乱,没有理清头绪,不打紧我会把项目源码给大家,跑一跑,debug一下,其实很简单,数据库也都包含在里面