一、添加依赖(pom.xml):
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--jstl标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--下面的tomcat支持jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
二、application.properties(热部署器):
#视图解析器
#视图解析器 前缀
spring.mvc.view.prefix=/WEB-INF/
#视图解析器 后缀
spring.mvc.view.suffix=.jsp
#mybatis数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/qademo?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=mysql
spring.datasource.password=0629
#mybatis映射文件位置
mybatis.mapper-locations=classpath:mapping/*.xml
三、在PagehelperdemoApplication启动类添加mapper接口的自动扫描的注解
@MapperScan("com.wanmait.pagehelperdemo.mapper")//mapper接口的自动扫描
四、mapper/questionTypeMapper接口:
public interface QuestionTypeMapper {
//查询所有的问题分类
List<QuestionType> findQuestionType();
}
五、resources/mapping/QuestionTypeMapper.xml(映射文件):
<!--查询所有的问题分类-->
<select id="findQuestionType" resultMap="BaseResultMap">
select id,title,question_count,sort from question_type
</select>
六、service/QuestionService接口:
public interface QuestionTypeService {
//pagehelper分页插件实现分页
PageInfo<QuestionType> findPage(int pageNum);
}
七、service/impl/QuestionTypeServiceImpl:
@Service("questionType")//spring的注解 //implements实现
public class QuestionTypeServiceImpl implements QuestionTypeService {
@Resource//spring的注解
private QuestionTypeMapper questionTypeMapper;
@Override//pagehelper分页插件实现分页
public PageInfo<QuestionType> findPage(int pageNum) {
int pageSize=2;//(1)pageNum第几页(2)pageSize每页显示条数
PageHelper.startPage(pageNum,pageSize);
//查询所有的问题分类
List<QuestionType> questionTypes=questionTypeMapper.findQuestionType();
//根据查询所有的问题分类实例化pageInfo
PageInfo<QuestionType> pageInfo=new PageInfo<>(questionTypes);
return pageInfo;
}
}
八、QuestionTypeServiceTest测试类:
@SpringBootTest
public class QuestionTypeServiceTest {
@Resource//spring的注解
private QuestionTypeService questionTypeService;
@Test//测试分页
public void testfindPage()
{
PageInfo<QuestionType> pageInfo=questionTypeService.findPage(1);
pageInfo.getList().forEach(System.out::println);
System.out.println(pageInfo.getPageNum());//第几页
System.out.println(pageInfo.getPages());//总页数
System.out.println(pageInfo.getPageSize());//每页显示条数
}
}
九、Controller/PagehelperController:
@Controller
public class PagehelperController {
@Resource//spring的注解
private QuestionTypeService questionTypeService;
@RequestMapping("pagehelper")
//指定属性名称,方法返回一个字符串,相当于model.addAttribute("pageInfo",pageInfo)
@ModelAttribute("pageInfo")
public PageInfo<QuestionType> pagehelper(Integer pageNum)
{
//pageNum第几页为null的时候,显示的第一页
if(pageNum==null)
{
pageNum=1;
}
return questionTypeService.findPage(pageNum);
}
}
十、webapp/WEB-INF/pagehelper.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<%String path = request.getContextPath();%>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1" width="1000">
<tr>
<td>ID</td>
<td>名称</td>
<td>问题的数量</td>
<td>分类</td>
</tr>
<c:forEach items="${requestScope.pageInfo.list}" var="questionType">
<tr>
<td>${questionType.id}</td>
<td>${questionType.title}</td>
<td>${questionType.questionCount}</td>
<td>${questionType.sort}</td>
</tr>
</c:forEach>
</table>
${requestScope.pageInfo.pages}/总条数 <!--(1)begin开始(2)end结束-->
<c:forEach begin="1" end="${requestScope.pageInfo.pages}" var="questionTypePage">
<a href="${path}/pagehelper?pageNum=${questionTypePage}">${questionTypePage}</a>
</c:forEach>${requestScope.pageInfo.pageNum}/${requestScope.pageInfo.pages}页
</body>
</html>
十一、网页显示效果: