您好,登录后才能下订单哦!
这篇文章给大家介绍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 --> <p 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> </p> <!-- 分页功能 End --> </td> </tr> <%--<tr>--%> <%--<td>${data.username}</td>--%> <%--<td>${data.password}</td>--%> <%--</tr>--%></table></body></html>
关于PageHelper中怎么实现服务器端分页功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。