0%

MYBAITS-分页插件PAGEHELPER

1:插入maven

1
2
3
4
5
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>

2:配置mybatis-config.xml

1
2
3
4
5
6
<plugins>
<plugin interceptor=“com.github.pagehelper.PageHelper”>
<!– 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库–>
<property name=“dialect” value=“mysql”/>
</plugin>
</plugins>

3:注入session

1
2
3
4
5
6
7
<!– springMyBatis完美整合,不需要mybatis的配置映射文件 –> 
<bean id=“sqlSessionFactory” class=“org.mybatis.spring.SqlSessionFactoryBean”>
<property name=“dataSource” ref=“dataSource” />
<!– 自动扫描mapping.xml文件 –>
<property name=“mapperLocations” value=“classpath:com/java/mapper/*.xml”></property>
<property name=“configLocation” value=“classpath:mybatis-config.xml”></property>
</bean>

4:分页

1
2
3
4
5
6
7
8
9
10
11
12
@RequestMapping(“/ss”)
public String page(@RequestParam(required = false,defaultValue = “1”,value = “pn”)Integer startPage,Model model) {
int pageSize =10;
System.out.println(“startPage==”+startPage);
PageHelper.startPage(startPage, pageSize);
List<Blog> blogList=blogService.Pagelist();
PageInfo<Blog> pageInfo = new PageInfo<Blog>(blogList);
System.out.println(“共有:”+ pageInfo.getLastPage());
model.addAttribute(“pageInfo”, pageInfo);
return “page”;

}

5:page.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core”; prefix=“c”%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”;>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”>
<title>Insert title here</title>
</head>
<body>
<c:forEach var=“blogTypeCount” items=“${pageInfo.list }“>
<li><span><a href=“#”>${blogTypeCount.title }</a></span></li>
</c:forEach>
<div class=“col-md-6”>
当前第 ${pageInfo.pageNum} 页.总共 ${pageInfo.pages} 页.一共 ${pageInfo.total} 条记录
</div>

<div class=“col-md-6”>
<nav aria-label=“Page navigation”>
<ul class=“pagination”>

<li><a href=“${pageContext.request.contextPath}/page.do?pn=1″>首页</a></li>

<!–上一页–>
<li>
<c:if test=“${pageInfo.hasPreviousPage}“>
<a href=“${pageContext.request.contextPath}/page.do?pn=${pageInfo.pageNum-1}“ aria-label=“Previous”>
<span aria-hidden=“true”>«</span>
</a>
</c:if>
</li>

<!–循环遍历连续显示的页面,若是当前页就高亮显示,并且没有链接–>
<c:forEach items=“${pageInfo.navigatepageNums}“ var=“page_num”>
<c:if test=“${page_num == pageInfo.pageNum}“>
<li class=“active”><a href=“#”>${page_num}</a></li>
</c:if>
<c:if test=“${page_num != pageInfo.pageNum}“>
<li><a href=“${pageContext.request.contextPath}/page.do?pn=${page_num}“>${page_num}</a></li>
</c:if>
</c:forEach>

<!–下一页–>
<li>
<c:if test=“${pageInfo.hasNextPage}“>
<a href=“${pageContext.request.contextPath}/page.do?pn=${pageInfo.pageNum+1}“
aria-label=“Next”>
<span aria-hidden=“true”>»</span>
</a>
</c:if>
</li>

<li><a href=“${pageContext.request.contextPath}/page?pn=${pageInfo.pages}“>尾页</a></li>
</ul>
</nav>
</div>


</body>
</html>