SSM+PageHelper实现分页查询
通过搭建ssm框架,然后通过mybatis的分页插件pagehelper-5.1.2.jar和jsqlparser-0.9.7.jar进行分页查询。
本博客摘抄于 https://www.cnblogs.com/smfx1314/archive/2018/04/23/8920278.html
除之前博客的SSM框架的整合的配置文件(https://www.cnblogs.com/hnzj/p/14855169.html)外
还要在mybatis-config.xml文件中增加
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <!-- 别名定义 --> 6 <typeAliases> 7 <package name="cn.edu.hnzj.po" /> 8 </typeAliases> 9 10 <!-- 引入 pageHelper插件 --> 11 <!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor --> 12 <plugins> 13 <plugin interceptor="com.github.pagehelper.PageInterceptor"> 14 <!-- reasonable:分页合理化参数,默认值为false。 当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。 15 默认false 时,直接根据参数进行查询。 --> 16 <property name="reasonable" value="true" /> 17 18 </plugin> 19 </plugins> 20 21 </configuration>
index.jsp
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="list">分页查询</a> </body> </html>
list.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <center> 12 <table width="200" border="1"> 13 <tr> 14 <th scope="col">ID</th> 15 <th scope="col">姓名</th> 16 <th scope="col">性别</th> 17 <th scope="col">城市</th> 18 </tr> 19 <c:forEach items="${pageInfo.list}" var="user"> 20 <tr> 21 <td>${user.id}</td> 22 <td>${user.username}</td> 23 <td>${user.sex}</td> 24 <td>${user.city}</td> 25 </tr> 26 </c:forEach> 27 </table> 28 29 <p>当前 ${pageInfo.pageNum}页,总${pageInfo.pages} 页,总 30 ${pageInfo.total} 条记录 31 32 </p> 33 34 <a href="list?pageNo=${pageInfo.firstPage}">第一页</a> 35 36 <c:if test="${pageInfo.hasPreviousPage}"> 37 <a href="list?pageNo=${pageInfo.pageNum-1}">上一页</a> 38 </c:if> 39 40 <c:if test="${pageInfo.hasNextPage}"> 41 <a href="list?pageNo=${pageInfo.pageNum+1}">下一页</a> 42 </c:if> 43 44 <a href="list?pageNo=${pageInfo.lastPage}">最后页</a> 45 </center> 46 </body> 47 </html>
UserController.java
1 package cn.edu.hnzj.controller; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.ModelMap; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMethod; 10 import org.springframework.web.bind.annotation.RequestParam; 11 12 import com.github.pagehelper.PageHelper; 13 import com.github.pagehelper.PageInfo; 14 15 import cn.edu.hnzj.po.User; 16 import cn.edu.hnzj.service.UserService; 17 18 @Controller 19 public class UserController { 20 @Autowired 21 private UserService pageService; 22 23 /** 24 * 分页查询 25 */ 26 @RequestMapping(value="/list",method=RequestMethod.GET) 27 public String pageList(ModelMap map,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){ 28 Integer pageSize=4;//每页显示记录数 29 //分页查询 30 PageHelper.startPage(pageNo, pageSize); 31 List<User> userList = pageService.list();//获取所有用户信息 32 PageInfo<User> pageInfo=new PageInfo<User>(userList); 33 map.addAttribute("pageInfo", pageInfo); 34 35 return "list"; 36 } 37 }
当一个方法中有多个查询语句时,只有紧跟在PageHelper.starPage()方法后的查询结果才会分页。


浙公网安备 33010602011771号