SSM+Maven+分页插件PageHelper(小白亲自试验)

转载:原出处:点击打开链接

大坑:

Controller下:

PageHelper.startPage必须放在List结果集的上面

  1.  PageHelper.startPage(page, 2);  
  2.           List<AdminUser> userList = auc.selectByList();  

一、POM.xml

[html]  view plain  copy
  1. <dependency>  
  2.     <groupId>com.github.pagehelper</groupId>  
  3.     <artifactId>pagehelper</artifactId>  
  4.     <version>4.1.6</version>  
  5. </dependency>  

踩坑:使用5.0以上的版本报错,能力有限还未理解什么问题,有大牛可以给评论下感激不尽。


二、Spring-*.xml

[html]  view plain  copy
  1.       
  2. <!-- 配置分页插件 -->  
  3.      <property name="plugins">  
  4.         <array>  
  5.           <bean class="com.github.pagehelper.PageHelper">  
  6.             <property name="properties">  
  7.               <value>  
  8.               <!-- 你使用的数据库类型 -->  
  9.                 dialect=mysql  
  10.                 reasonable=true  
  11.               </value>  
  12.             </property>  
  13.           </bean>  
  14.         </array>  
  15.       </property>  


三、Controller

[java]  view plain  copy
  1. @Controller  
  2. @RequestMapping(value = "/uu", method = { RequestMethod.GET, RequestMethod.POST })  
  3. public class UserTest {  
  4.     @Autowired  
  5.     private AdminUserService auc;  
  6.     @RequestMapping("/userList")  
  7.       public String userList(@RequestParam(required=true,defaultValue="1") Integer page,HttpServletRequest request,Model model){  
  8.           //page默认值是1,pageSize默认是10,我写的是2 意思是从第1页开始,每页显示2条记录。  

  1.           PageHelper.startPage(page, 2);  
  1.           List<AdminUser> userList = auc.selectByList();  
  1.           System.out.println(userList);  
  2.           PageInfo<AdminUser> p=new PageInfo<AdminUser>(userList);  
  3.           model.addAttribute("page", p);  
  4.           model.addAttribute("userList",userList);  
  5.           return "/admin/test.jsp";  
  6.       }  
  7.   
  8. }  

导入

[java]  view plain  copy
  1. import java.util.List;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.ui.Model;  
  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;  


四、Service 

[java]  view plain  copy
  1. List<AdminUser> selectByList();  


五、ServiceImpl

[java]  view plain  copy
  1. @Override  
  2. public List<AdminUser> selectByList() {  
  3.     // TODO Auto-generated method stub  
  4.     return adminUserMapper.selectByList();  
  5. }  


六、Dao

[java]  view plain  copy
  1. List<AdminUser> selectByList();  

七、Mapper.xml

[html]  view plain  copy
  1. <select id="selectByList" resultType="com.*.*.entity.AdminUser">  
  2.      select  *  from adminuser T  
  3. </select>  
[html]  view plain  copy
  1. resultType="com.*.*.entity.AdminUser  

上面是你的实体类


八、Jsp

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()  
  5.             + path + "/";  
  6. %>  
  7. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11. <head>  
  12. <base href="<%=basePath%>">  
  13.   
  14. <title>My JSP 'test.jsp' starting page</title>  
  15.   
  16. <meta http-equiv="pragma" content="no-cache">  
  17. <meta http-equiv="cache-control" content="no-cache">  
  18. <meta http-equiv="expires" content="0">  
  19. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20. <meta http-equiv="description" content="This is my page">  
  21. <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->  
  22. <link rel="stylesheet"  
  23.     href="https  
  24. ://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"  
  25.     integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"  
  26.     crossorigin="anonymous">  
  27.   
  28. <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->  
  29. <link rel="stylesheet"  
  30.     href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"  
  31.     integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"  
  32.     crossorigin="anonymous">  
  33.   
  34. <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->  
  35. <script  
  36.     src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"  
  37.     integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"  
  38.     crossorigin="anonymous"></script>  
  39.   
  40. </head>  
  41.   
  42. <body>  
  43.     <center>  
  44.         <table width="200" border="1" class="table table-striped">  
  45.             <tr>  
  46.                 <th scope="col">序号</th>  
  47.                 <th scope="col">ID</th>  
  48.                 <th scope="col">姓名</th>  
  49.                 <th scope="col">密码</th>  
  50.                 <th scope="col">年龄</th>  
  51.             </tr>  
  52.             <c:forEach begin="0" step="1" items="${userList}" var="list"  
  53.                 varStatus="userlist">  
  54.                 <tr>  
  55.                     <td></td>  
  56.                     <td>${list.userId}</td>  
  57.                     <td>${list.userName}</td>  
  58.                     <td>${list.userPassword}</td>  
  59.                     <td></td>  
  60.                 </tr>  
  61.             </c:forEach>  
  62.         </table>  
  63.         <p>当前表格共${page.pages}页、${page.total}条记录</p>  
  64.   
  65.           
  66. <nav aria-label="Page navigation">  
  67.   <ul class="pagination">  
  68.     <li>  
  69.       <a href="<%=request.getContextPath()%>/uu/userList?page=${page.firstPage}" aria-label="Previous">  
  70.         <span aria-hidden="true">«</span>  
  71.       </a>  
  72.     </li>  
  73.     <c:forEach var="s" begin="1" end="${page.pages}">  
  74.     <li><a href="<%=request.getContextPath()%>/uu/userList?page=${s}">${s}</a></li>  
  75.    </c:forEach>  
  76.     <li>  
  77.       <a href="<%=request.getContextPath()%>/uu/userList?page=${page.lastPage}" aria-label="Next">  
  78.         <span aria-hidden="true">»</span>  
  79.       </a>  
  80.     </li>  
  81.   </ul>  
  82. </nav>  
  83.           
  84.           
  85.     </center>  
  86. </html>  

九、效果图(没有多少字段也没展示多少字段 )

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值