1. 程式人生 > 實用技巧 >ssm中使用pagehelper

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/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<!-- JS, Popper.js, and jQuery -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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>