在springBoot里通过Pagehelper分页插件实现分页功能的过程

一、添加依赖(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>

十一、网页显示效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值