最新消息: 新版网站上线了!!!

PageHelper插件实现服务器端分页功能

本文实例为大家分享了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>
<h1>User</h1>
<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插件实现服务器端分页功能