springboot集成mybatis的分页插件pageHelper

1、添加分页插件依赖文件
<properties>
        <java.version>1.7</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- SpringBoot 集成mybatis相关的依赖包 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- 分页插件pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- 分页插件pagehelper -->
    </dependencies>
2、配置application.yml文件
spring:
  mvc:
    view:
      prefix: /
      suffix: .jsp
  datasource:
    url: jdbc:mysql://localhost:3306/myrec?characterEncoding=utf8&useSSL=true
    username: root
    password: m123456
    driver-class-name: com.mysql.jdbc.Driver
       
#配置分页插件pagehelper
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
3,控制器层controller的使用
	@Autowired
    PostInfoService postInfo;
    
    @RequestMapping("/info")
    public String getAll(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model){
        //获取第1页,5条内容,默认查询总数count
        /* 第一个参数是第几页;第二个参数是每页显示条数 */
        PageHelper.startPage(pn, 3);
        List<PostInfor> postIn = postInfo.getAll();
        System.out.println(postIn+"===========");
        //用PageInfo对结果进行包装
        
        PageInfo<PostInfor> page = new PageInfo<PostInfor>(postIn);
        model.addAttribute("pageInfo", page);
        return "index";
    }
4、前端处理数据的index.html页面-分页部分
<!-- 分页 -->
<div class="ui circular labels" style="float: right;">
    <a class="ui label">当前第 ${pageInfo.pageNum }页,总${pageInfo.pages }
页,总 ${pageInfo.total } 条记录</a>
    <a class="ui label" href="info?pn=1">首页</a>
    <c:if test="${pageInfo.hasPreviousPage }">
        <a class="ui label" href="info?pn=${pageInfo.pageNum-1 }">&laquo;</a>
    </c:if>

    <c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
        <c:if test="${page_Num == pageInfo.pageNum }">
            <a class="ui label" href="info?pn=${page_Num}">${page_Num}</a>
        </c:if>
        <c:if test="${page_Num != pageInfo.pageNum }">
            <a class="ui label" href="info?pn=${page_Num}">${page_Num }</a>
        </c:if>
    </c:forEach>
    
    <c:if test="${pageInfo.hasNextPage }">
        <a class="ui label" href="info?pn=${pageInfo.pageNum+1 }">&raquo;</a>
    </c:if>
    <a class="ui label" href="info?pn=${pageInfo.pages}">末页</a>
</div>
<!-- 分页 end-->

温馨提示:在使用pageHelper的过程中如遇到一些问题,可关注左边栏我的个人公众号进行询问。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值