springboot使用pagehelper进行分页

上次的博客项目,使用到了分页,这里总结一下

1.项目环境
IDE:IDEA
语言:java
框架:springboot
模板引擎:thymeleaf

2.效果
在这里插入图片描述
3.pom.xml

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

4.application.properties

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params.count=countSql

5.后端

  //查博客
    @RequestMapping("/QueryBlog")
    public String QueryBlog(Model m ,@RequestParam(value="start",
            defaultValue="1") Integer start, @RequestParam(value="size",defaultValue="3") Integer size){
//设置分页的起始页面和页面大小
        PageHelper.startPage(start ,size);
        //查询到的博客信息
        List<blog_info> list = bms.queryAllBlog();
        //将查询到的博客信息放到pageInfo中去,进行分页
        PageInfo<blog_info> page = new PageInfo<blog_info>(list);
        m.addAttribute("page" ,page);
        m.addAttribute("list",list);
        return "blogLater/adminMain";
    }

6.前端取值

<table>
        <tr>
        <td>编号</td>
        <td>标题</td>
        <td>预览图</td>
        <td>浏览量</td>
        <td>状态</td>
        <td>博客分类</td>
        <td>添加时间</td>
        </tr>
        <tr th:each="c:${list}">
            <td th:text="${c.blogId}"></td>
            <td th:text="${c.blogTitle}"></td>
            <td><img th:src="'img/blogManage/'+${c.blogCoverImage}" width="200px" height="133px"/></td>
            <td th:text="${c.blogReadNumber}"></td>
            <td th:text="${c.blogState}"></td>
            <td th:text="${c.blogCategoryId}"></td>
            <td th:text="${c.createTime}"></td>
       
        </tr>
    </table>

<di">
    <button  class="btn-info" value="" style="width: 70px;height: 30px;font-size: 18px">
        <a th:href="@{/QueryBlog(start=1)}">首页</a>
    </button
 
        <a th:href="@{/QueryBlog(start=${page.getPageNum()-1})}">上一页</a>
    </button>
 
        <a th:href="@{/QueryBlog(start=${page.getPageNum()+1})}">下一页</a>
    </button>
        <a th:href="@{/QueryBlog(start=${page.getPages()})}">尾页</a>
   

        <span>显示第一到最后一条,共
            <sapn th:text="${page.getPages()}"></sapn>,当前第
            <span th:text="${page.getPageNum()}></span></span>

7.总结
看起来很简单,但是我做这个分页时遇到了各种各样的困难,有的pom.xml还要导入其他的包,有的还要配置。反正我这个方式是可以成功分页的,如果不想用工具来分页的话,自己写sql来分页吗。

有什么不懂的看一下我的源码,关于分页的一部分呢,这个源码是一个半成品,但是可以运行(只要把环境搭好).,下面的链接也不知道可以不可以用,直接去我的主页找,hk_blog_2

//download.csdn.net/download/m0_45025658/12111291

访问地址:http://localhost:8016/enterAdminLogin
账号:adminxyz
密码: adminxyz

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值