码迷,mamicode.com
首页 > 其他好文 > 详细

SSM+PageHelper实现分页查询

时间:2021-06-20 17:34:55      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:col   tag   title   cto   page   reac   new   list   tis   

通过搭建ssm框架,然后通过mybatis的分页插件pagehelper-5.1.2.jar和jsqlparser-0.9.7.jar进行分页查询。

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 }

技术图片

 

SSM+PageHelper实现分页查询

标签:col   tag   title   cto   page   reac   new   list   tis   

原文地址:https://www.cnblogs.com/hnzj/p/14903853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!