听说PageHelper很流弊,今天它来了。
- 1.导包:
jsqlparser-0.9.5.jar
pagehelper-5.0.3.jar
免费站内资源:
pagehelper-5.0.3.jar
jsqlparser-0.9.5.jar
- 2.配置走一走
注意Plguins插入的位置,以及配置文件所在位置,我就是不小心留了两配置文件,结果折腾了好久。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="dbconfig.properties"></properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 这是配置二级缓存的,一级缓存一直存在 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<!-- <property name="param1" value="value1"/> -->
</plugin>
</plugins>
</configuration>
- 3.撸代码
Controller:
package com.EzerbelCN.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.EzerbelCN.bean.Teacher;
import com.EzerbelCN.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class TeacherController{
@Autowired
TeacherService teacherService;
@RequestMapping("/getTeacherById")
public String getTeacherHandler(@RequestParam(value ="id",defaultValue="1") Integer id,Model model) {
Teacher teacher = teacherService.getTeacherById(1);
model.addAttribute("teacher", teacher);
return "Success";
}
@RequestMapping("/getTeachers")
public String getTeachersHandler(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model)
{
PageHelper.startPage(pn,5);
//紧跟它的一个查询就是一个分页查询
List<Teacher> list = teacherService.getTeachers();
//第二个参数传入要连续显示的页码数
PageInfo<Teacher> info = new PageInfo<>(list,6);
System.out.println("info.getPageNum == " + info.getPageNum());
System.out.println("info.getPages == " + info.getPages());
System.out.println("info.getTotal == " + info.getTotal());
System.out.println("info.getSize == " + info.getSize());
System.out.println("list.size = = = " + list.size());
System.out.println("pn ======= " + pn);
model.addAttribute("info",info);
return "Teachers";
}
}
Teachers.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>All Teachers Here</h1><br/>
<table cellpadding="5" cellspacing="0" border="1">
<tr>
<th>id</th>
<th>name</th>
<th>course</th>
</tr>
<c:forEach items="${info.list}" var="teacher">
<tr>
<td>${teacher.id}</td>
<td>${teacher.name }</td>
<td>${teacher.course}</td>
</tr>
</c:forEach>
<tr>
<td colspan="4">
<a href="getTeachers?pn=1">首页</a>
<a href="getTeachers?pn=${info.prePage}">上一页</a>
<%-- <a>${info.pageNum}</a> --%>
<c:forEach items="${info.navigatepageNums}" var="num">
<c:if test="${info.pageNum == num}">
<a>【${num}】</a>
</c:if>
<c:if test="${info.pageNum != num}">
<a href="getTeachers?pn=${num}">${num}</a>
</c:if>
</c:forEach>
<a href="getTeachers?pn=${info.nextPage}">下一页</a>
<!-- 这里用lastPage竟然不对?! -->
<a href="getTeachers?pn=${info.pages}">末页</a>
</td>
</tr>
</table>
</body>
</html>
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="getTeachers">查询AllTeachers</a><br/>
</body>
</html>
- 4.测试: