您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關使用PageHelper插件怎么實現服務器端分頁功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
一、添加依賴
在.pom文件中添加
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.5.1</version> </dependency>
二、添加配置/strong>
在spring-mybaits.xml文件中添加插件
<!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapping/*.xml"></property> <!--分頁插件--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql reasonable=true </value> </property> </bean> </array> </property> <!--分頁--> </bean>
三、controller代碼
//分頁展示用戶 @RequestMapping(value = "/user.do") public ModelAndView test1(HttpServletRequest request, HttpServletResponse response, ModelMap mode) throws Exception { ModelAndView modelAndView = new ModelAndView(); int pageNumber = 1; try{ if(request.getParameter("pageNow").equals(null)){ System.out.println("參數空"); }else { pageNumber = Integer.parseInt(request.getParameter("pageNow")); } }catch (Exception e){ //處理空針錯誤 } System.out.println("請求參數"+request.getParameter("pageNow")); PageInfo<user> pageInfo = userService.selectUserByPage(pageNumber,5); // Map<String, Object> map = new HashMap<String, Object>(); // List<Object> data = new ArrayList<Object>(); // for(user admin : pageInfo.getList()){ // Map<String, Object> obj = new HashMap<String, Object>(); // obj.put("id", admin.getId()); // obj.put("username", admin.getUsername()); // obj.put("password", admin.getPassword()); // obj.put("enable", admin.getEnable()); // obj.put("id_card", admin.getIdCard()); // obj.put("phone", admin.getPhone()); // obj.put("address", admin.getAddress()); // obj.put("nick_name", admin.getAddress()); // data.add(obj); // } modelAndView.addObject("pageInfo",pageInfo); //jsp 根據users來傳遞信息 System.out.println("成功啦"); modelAndView.setViewName("/test/user"); return modelAndView; }
四、Service方法
//分頁獲取用戶信息 public PageInfo<User> selectUserByPage(Integer pageNum, Integer pageSize) { PageHelper.startPage(pageNum, pageSize); // 所有用戶信息 List<User> authAdmins = userMapper.selectUsers(); if(authAdmins == null){ return null; } PageInfo<User> pageInfo = new PageInfo<User>(authAdmins); return pageInfo; }
五、jsp顯示
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/12/2 0002 Time: 11:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>user</title> </head> <body> <h2>User</h2> <table> <c:forEach items="${pageInfo.list}" var="u"> <%--屬性值映射mapper 中的property--%> <tr> <td>${u.id}</td> <td>${u.username}</td> <td>${u.password}</td> <td>${u.enable}</td> <td>${u.idCard}</td> <td>${u.phone}</td> <td>${u.address}</td> <td>${u.nickName}</td> </tr> </c:forEach> <tr> <td colspan="6"> <!-- 分頁功能 start --> <div align="center"> <font size="2">共 ${pageInfo.pageSize} 頁</font> <font size="2">第 ${pageInfo.pageNum} 頁</font> <a href="${pageContext.request.contextPath}/user.do?pageNow=1" rel="external nofollow" rel="external nofollow" >首頁</a> <c:choose> <c:when test="${pageInfo.pageNum - 1 > 0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum - 1}" rel="external nofollow" >上一頁</a> </c:when> <c:when test="${pageInfo.pageNum - 1 <= 0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=1" rel="external nofollow" rel="external nofollow" >上一頁</a> </c:when> </c:choose> <%--pageSize共 頁--%> <%--pageNum 第 頁--%> <%--pagetotal 共 條--%> <c:choose> <c:when test="${pageInfo.pageSize==0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum}" rel="external nofollow" >下一頁</a> </c:when> <c:when test="${pageInfo.pageNum + 1 < pageInfo.pageSize}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum + 1}" rel="external nofollow" >下一頁</a> </c:when> <c:when test="${pageInfo.pageNum + 1 >= pageInfo.pageSize}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a> </c:when> </c:choose> <c:choose> <c:when test="${pageInfo.pageSize==0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a> </c:when> <c:otherwise> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a> </c:otherwise> </c:choose> </div> <!-- 分頁功能 End --> </td> </tr> <%--<tr>--%> <%--<td>${data.username}</td>--%> <%--<td>${data.password}</td>--%> <%--</tr>--%> </table> </body> </html>
以上就是使用PageHelper插件怎么實現服務器端分頁功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。