点击查看异步分页
在mybatis-config.xml 中配置分页
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 方言:用来指定数据库类型 -->
<property name="helperDialect" value="mysql"/>
<!--当页数值小于第一页时直接显示第一页 当页数值大于最后一页时只显示最后一页-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
工具类
public class PageUtils {
//设置每页显示几条数据
public static final int PAGE_SIZE=2;
//设置每页导航页显示到几个
public static final int PAGE_CONTROL=5;
//自己定义的工具类 因为会用到很多的不止一个模块的分页 好多模块都能用到
//但是前提是条件查询前缀必须为search_
public static String parseParamMapToQueryStr(Map<String, Object> paramMap,String prefix){
StringBuilder builder= new StringBuilder();
Set<Map.Entry<String, Object>> entries = paramMap.entrySet();
for (Map.Entry<String, Object> entry : entries) {
//因为WebUtils.getParametersStartingWith()工具类
//默认截取页面前缀为指定值后边的字符串作为key 值为value
//所以我们获取页面上name的前缀prefix 拼接上 ,就是页面的name;
// 才可以再次让WebUtils.getParametersStartingWith()获取到数据
//就可以完成分页携带的参数了 是换页时携带数据;
builder.append("&").append(prefix).append(entry.getKey()).append("=").append(entry.getValue());
}
return builder.toString();
}
}
controller
@Controller
@RequestMapping("/customer")
public class CustomerController {
`` //同步分页条件查询用户
@RequestMapping(value = "/selcon",method = RequestMethod.GET)
public ModelAndView selectCondition(HttpServletRequest request, @RequestParam(value="pageNum",defaultValue = "1")String pageNum){
//获取请求行信息
String requestURI = request.getRequestURI();
//利用WebUtils.getParametersStartingWith()工具类
// 传入参数request 并截取页面前缀为指定值后边的字符串作为key 值为value
Map<String, Object> paramMap = WebUtils.getParametersStartingWith(request, "search_");
//用于后边工具类的使用 工具类需要拼接上前缀才是页面name的名称
String prefix="search_";
//创建ModelAndView
ModelAndView model=new ModelAndView("customer");
//将请求头放入ModelAndView 中传入页面
model.addObject("requestURI",requestURI);
//获取service传过来的数据
PageInfo<Customer> pageInfo = customers.getSelectCondition(pageNum,paramMap);
//吧pageInfo放入ModelAndView 传入页面
model.addObject("pageInfo",pageInfo);
//调取工具类,主要用于条件查询数据,是换页时携带数据;
String queryStr= PageUtils.parseParamMapToQueryStr(paramMap,prefix);
model.addObject("queryStr",queryStr);
//吧参数传入后台 用于回显
model.addObject("cid",paramMap.get("cid"));
model.addObject("keyword",paramMap.get("keyword"));
model.addObject("orderby",paramMap.get("orderby"));
return model;
}`
service
```java
//条件查询
@Override
public PageInfo<Customer> getSelectCondition(String pageNum, Map<String, Object> paramMap) {
//分页显示在第几页,和每页显示记录数
PageHelper.startPage(Integer.parseInt(pageNum),PageUtils.PAGE_SIZE);
//传入后台map进行查询数据
List<Customer> customers = customerMapper.selectCondition(paramMap);
//吧查询的数据放入PageInfo中,并设置导航页
PageInfo<Customer>page=new PageInfo<Customer>(customers,PageUtils.PAGE_CONTROL);
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("总记录数:"+pageInfo.getTotal());
System.out.println("当前页:"+pageInfo.getPageNum());
System.out.println("是否为第一页:"+pageInfo.isIsFirstPage());
System.out.println("是否为最后一页:"+pageInfo.isIsLastPage());
System.out.println("上一页:"+pageInfo.getPrePage());
System.out.println("下一页:"+pageInfo.getNextPage());
System.out.println("是否有上一页:"+pageInfo.isHasPreviousPage());
System.out.println("是否下一页:"+pageInfo.isHasNextPage());
System.out.println("pageSize:"+pageInfo.getPageSize());
System.out.println("列表数据:"+pageInfo.getList());
int[] navigatepageNums = pageInfo.getNavigatepageNums();
System.out.println("有效导航页:"+Arrays.toString(navigatepageNums))
return page;
}
mapper
//条件查询
public List<Customer> selectCondition(Map<String, Object> paramMap);
<!--条件查询信息-->
<select id="selectCondition" resultType="com.ujiuye.customer.bean.Customer">
select * from customer
<where>
<if test="cid!=null and cid==0">
comname like '%${keyword}%' or companyperson like '%${keyword}%'
</if>
<if test="cid!=null and cid==1">
comname like '%${keyword}%'
</if>
<if test="cid!=null and cid==2">
companyperson like '%${keyword}%'
</if>
<if test="orderby!=null and orderby==1">
order by id desc
</if>
</where>
</select>
抽取的分页工具类
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<!--抽取的分页 供多个模块使用 前提是后台的多个分页模块传入页面PageInfo
对象 的key值名称相同即可 requestURI 存入请求行名称的key 也得相同 -->
<body>
<td height="28" colspan="7">
<a href="${requestURI}?pageNum=1${queryStr}">首页</a>
<a href="${requestURI}?pageNum=${pageInfo.pageNum-1}${queryStr}">上一页</a>
<c:forEach items="${pageInfo.navigatepageNums}" var="num">
<c:if test="${num == pageInfo.pageNum}">
[${num}]
</c:if>
<c:if test="${num != pageInfo.pageNum}">
<a href="${requestURI}?pageNum=${num}${queryStr}">${num}</a>
</c:if>
</c:forEach>
<a href="${requestURI}?pageNum=${pageInfo.pageNum+1}${queryStr}">下一页</a>
<a href="${requestURI}?pageNum=${pageInfo.pages}${queryStr}">末页</a>
跳转到<input id="changeNum" value="${pageInfo.pageNum}" size="1px">页
<input type="button" id="submit" value="跳转">
<script type="application/javascript" src="${pageContext.request.contextPath}/static/js/jquery-1.8.3.js"></script>
<script type="application/javascript">
$("#submit").click(function () {
var page=$("#changeNum").val();
var reg = /^[1-9]\d*$/;
if(!reg.test(page)){
alert("请输入正确页码");
return ;
}
window.location.href="${requestURI}?pageNum="+page+"${queryStr}";
});
</script>
</td>
</body>
</html>
jsp
<c:forEach items="${pageInfo.list}" var="cust" varStatus="index">
引入分页
<%@ include file="page.jsp"%>