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

一、添加依赖
在.pom文件中添加

    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>3.5.1version>
    dependency>

二、添加配置
在spring-mybaits.xml文件中添加插件

     
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <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 pageInfo = userService.selectUserByPage(pageNumber,5);

//        Map map = new HashMap();
//        List data = new ArrayList();
//        for(user admin : pageInfo.getList()){
//            Map obj = new HashMap();
//            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 selectUserByPage(Integer pageNum, Integer pageSize) {

    PageHelper.startPage(pageNum, pageSize);
    // 所有用户信息
    List authAdmins = userMapper.selectUsers();
    if(authAdmins == null){
        return null;
    }
    PageInfo pageInfo = new PageInfo(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>usertitle>
head>
<body>
<h1>Userh1>
<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">
            
            <div align="center">
                <font size="2">共 ${pageInfo.pageSize} 页font> <font size="2">第
                ${pageInfo.pageNum} 页font> <a href="${pageContext.request.contextPath}/user.do?pageNow=1">首页a>
                <c:choose>
                    <c:when test="${pageInfo.pageNum - 1 > 0}">
                        <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum - 1}">上一页a>
                    c:when>
                    <c:when test="${pageInfo.pageNum - 1 <= 0}">
                        <a href="${pageContext.request.contextPath}/user.do?pageNow=1">上一页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}">下一页a>
                    c:when>
                    <c:when test="${pageInfo.pageNum + 1 < pageInfo.pageSize}">
                        <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum + 1}">下一页a>
                    c:when>
                    <c:when test="${pageInfo.pageNum + 1 >= pageInfo.pageSize}">
                        <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}">下一页a>
                    c:when>
                c:choose>
                <c:choose>
                    <c:when test="${pageInfo.pageSize==0}">
                        <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}">尾页a>
                    c:when>
                    <c:otherwise>
                        <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}">尾页a>
                    c:otherwise>
                c:choose>
            div>
            
        td>

    tr>
    <%----%>
        <%--${data.username}--%>
        <%--${data.password}--%>
    <%----%>
table>
body>
html>

你可能感兴趣的:(Java,web)