SSM+Maven+分页插件PageHelper(小白亲自试验)
转载:原出处:点击打开链接
大坑:
Controller下:
PageHelper.startPage必须放在List结果集的上面
- PageHelper.startPage(page, 2);
- List<AdminUser> userList = auc.selectByList();
一、POM.xml
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- <version>4.1.6</version>
- </dependency>
踩坑:使用5.0以上的版本报错,能力有限还未理解什么问题,有大牛可以给评论下感激不尽。
二、Spring-*.xml
- <!-- 配置分页插件 -->
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageHelper">
- <property name="properties">
- <value>
- <!-- 你使用的数据库类型 -->
- dialect=mysql
- reasonable=true
- </value>
- </property>
- </bean>
- </array>
- </property>
三、Controller
- @Controller
- @RequestMapping(value = "/uu", method = { RequestMethod.GET, RequestMethod.POST })
- public class UserTest {
- @Autowired
- private AdminUserService auc;
- @RequestMapping("/userList")
- public String userList(@RequestParam(required=true,defaultValue="1") Integer page,HttpServletRequest request,Model model){
- //page默认值是1,pageSize默认是10,我写的是2 意思是从第1页开始,每页显示2条记录。
- PageHelper.startPage(page, 2);
- List<AdminUser> userList = auc.selectByList();
- System.out.println(userList);
- PageInfo<AdminUser> p=new PageInfo<AdminUser>(userList);
- model.addAttribute("page", p);
- model.addAttribute("userList",userList);
- return "/admin/test.jsp";
- }
- }
导入
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
四、Service
- List<AdminUser> selectByList();
五、ServiceImpl
- @Override
- public List<AdminUser> selectByList() {
- // TODO Auto-generated method stub
- return adminUserMapper.selectByList();
- }
六、Dao
- List<AdminUser> selectByList();
七、Mapper.xml
- <select id="selectByList" resultType="com.*.*.entity.AdminUser">
- select * from adminuser T
- </select>
- resultType="com.*.*.entity.AdminUser
上面是你的实体类
八、Jsp
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'test.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
- <link rel="stylesheet"
- href="https
- ://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
- integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
- crossorigin="anonymous">
- <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
- <link rel="stylesheet"
- href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
- integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
- crossorigin="anonymous">
- <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
- <script
- src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"
- integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
- crossorigin="anonymous"></script>
- </head>
- <body>
- <center>
- <table width="200" border="1" class="table table-striped">
- <tr>
- <th scope="col">序号</th>
- <th scope="col">ID</th>
- <th scope="col">姓名</th>
- <th scope="col">密码</th>
- <th scope="col">年龄</th>
- </tr>
- <c:forEach begin="0" step="1" items="${userList}" var="list"
- varStatus="userlist">
- <tr>
- <td></td>
- <td>${list.userId}</td>
- <td>${list.userName}</td>
- <td>${list.userPassword}</td>
- <td></td>
- </tr>
- </c:forEach>
- </table>
- <p>当前表格共${page.pages}页、${page.total}条记录</p>
- <nav aria-label="Page navigation">
- <ul class="pagination">
- <li>
- <a href="<%=request.getContextPath()%>/uu/userList?page=${page.firstPage}" aria-label="Previous">
- <span aria-hidden="true">«</span>
- </a>
- </li>
- <c:forEach var="s" begin="1" end="${page.pages}">
- <li><a href="<%=request.getContextPath()%>/uu/userList?page=${s}">${s}</a></li>
- </c:forEach>
- <li>
- <a href="<%=request.getContextPath()%>/uu/userList?page=${page.lastPage}" aria-label="Next">
- <span aria-hidden="true">»</span>
- </a>
- </li>
- </ul>
- </nav>
- </center>
- </html>
九、效果图(没有多少字段也没展示多少字段 )