您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)使用PageHelper插件怎么實(shí)現(xiàn)服務(wù)器端分頁功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
一、添加依賴
在.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"/> <!-- 自動(dòng)掃描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("參數(shù)空"); }else { pageNumber = Integer.parseInt(request.getParameter("pageNow")); } }catch (Exception e){ //處理空針錯(cuò)誤 } System.out.println("請(qǐng)求參數(shù)"+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 根據(jù)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插件怎么實(shí)現(xiàn)服務(wù)器端分頁功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。