zoukankan      html  css  js  c++  java
  • ssm中使用pagehelper

     pagehelper的使用

          1, 引入依赖

    <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.10</version>
    </dependency>

          2, applicationContext.xml文件中, 在sqlSessionFatctoryBean配置中添加plugins 

             

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--daomapper文件的对应位置-->
    <property name="mapperLocations">
    <list>
    <value>classpath:com/qf/dao/daoMapper/*.xml</value>
    </list>
    </property>
    <!--为daomapper文件中的实体,定义缺省包路径-->
    <property name="typeAliasesPackage" value="com.qf.entity"/>
    <!-- 传入PageHelper 分页插件 -->
    <property name="plugins">
    <array>
    <!-- 传入插件的对象 -->
    <bean class="com.github.pagehelper.PageInterceptor">
    <property name="properties">
    <props>
    <!--页码超出范围自动修正-->
    <prop key="reasonable">true</prop>
    </props>
    </property>
    </bean>
    </array>
    </property>
    </bean>

    3. service层的实现类中, 使用
    PageHelper.startPage(页数pageNum, 每页条数pageSize);
    @Override
    public PageInfo<User> queryUsersByPageHelper(Page page) {
    PageHelper.startPage(page.getNum(), page.getSize());//pagehelper加在前面会自动添加limit,而不影响性能
    List<User> users = userDao.queryUsers();
    PageInfo<User> pageInfo = new PageInfo<>(users);
    return pageInfo;
    }

    4controller层中
    @GetMapping("/users")
    public String queryUsers(Page page, Model model){//因为在事务中配置了query开头做查询,这里必须用query开头
    System.out.println("queryUsers");
    PageInfo<User> pageInfo = userService.queryUsersByPageHelper(page);
    System.out.println(pageInfo);
    model.addAttribute("pageInfo",pageInfo);
    return "user2";
    }

    5页面部分
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%--
    Created by IntelliJ IDEA.
    User: 17809203406
    Date: 2020/12/5
    Time: 10:13
    To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
    <title>Title</title>
    <!-- CSS only -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <!-- JS, Popper.js, and jQuery -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    <style>
    th, td{
    text-align: center;
    }
    .pagination{
    align-items: center;
    justify-content: center;
    }
    </style>
    </head>
    <body>
    <table class="table table-bordered">
    <thead>
    <tr>
    <%--<th scope="col">#</th>--%>
    <th scope="col">id</th>
    <th scope="col">name</th>
    <th scope="col">age</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${pageInfo.list}" var="user">
    <tr>
    <td>${user.id}</td>
    <td>${user.name}</td>
    <td>${user.age}</td>
    </tr>
    </c:forEach>


    </tbody>
    </table>
    <nav aria-label="Page navigation example">
    <ul class="pagination">
    <c:if test="${pageInfo.isFirstPage==true}">
    <li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>
    </c:if>
    <c:if test="${pageInfo.isFirstPage==false}">
    <li class="page-item"><a class="page-link" href="${pageContext.request.contextPath}/users?num=${pageInfo.pageNum-1}">Previous</a></li>
    </c:if>
    <c:forEach begin="1" end="${pageInfo.pages}" var="page">
    <c:if test="${page==pageInfo.pageNum}">
    <li class="page-item active"><a class="page-link" href="${pageContext.request.contextPath}/users?num=${page}">${page}</a></li>
    </c:if>
    <c:if test="${page!=pageInfo.pageNum}">
    <li class="page-item"><a class="page-link" href="${pageContext.request.contextPath}/users?num=${page}">${page}</a></li>
    </c:if>
    </c:forEach>


    <c:if test="${pageInfo.isLastPage==true}">
    <li class="page-item disabled"><a class="page-link" href="#">Next</a></li>
    </c:if>
    <c:if test="${pageInfo.isLastPage==false}">
    <li class="page-item"><a class="page-link" href="${pageContext.request.contextPath}/users?num=${pageInfo.pageNum+1}">Next</a></li>
    </c:if>

    </ul>
    </nav>
    </body>
    </html>
  • 相关阅读:
    python基础-元组类型
    python基础-列表类型
    python基础-字符串类型
    python基础-数字类型
    python基础-循环
    python基础-短路和拷贝
    python基础-人机交互和运算符
    python基础-垃圾回收机制
    python基础-python介绍
    UCF Local Programming Contest 2017(2020-4-6)
  • 原文地址:https://www.cnblogs.com/dangdanghepingping/p/14091993.html
Copyright © 2011-2022 走看看