springboot+thymeleaf之pageHelper分页

依赖

<!--	分页查询	-->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.3</version>
		</dependency>

application.yml配置

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true

controller

    @RequestMapping({"/","index"})
    public String index(Model model) {
        //index页博客分类信息
        List<Type> typeList = typeService.selectAll();

        Integer pageNum = 1;
        //按照时间排序前提下按照浏览排序查询所有博客
        //分页操作
        PageHelper.startPage(pageNum, 2);
        List<UserBlogInfo> userBlogInfos = blogService.selectAllBlog();
        PageInfo<UserBlogInfo> pageInfo = new PageInfo<UserBlogInfo>(userBlogInfos);

        //页面总数
        Integer pageCount = (blogService.selectAllBlogCount()/2)+(blogService.selectAllBlogCount()%2!=0?1:0);

        //将数据传给页面
        model.addAttribute("typeList",typeList);
        model.addAttribute("userBlogInfos",userBlogInfos);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNum",pageNum);

        return "index";
    }


	@RequestMapping("/index/{pageNum}")
    public String blogPage(@PathVariable("pageNum") Integer pageNum, Model model){
        //index页博客分类信息
        List<Type> typeList = typeService.selectAll();

        //按照时间排序前提下按照浏览排序查询所有博客
        //分页操作
        PageHelper.startPage(pageNum, 2);
        List<UserBlogInfo> userBlogInfos = blogService.selectAllBlog();
        PageInfo<UserBlogInfo> pageInfo = new PageInfo<UserBlogInfo>(userBlogInfos);

        //页面总数
        Integer pageCount = (blogService.selectAllBlogCount()/2)+(blogService.selectAllBlogCount()%2!=0?1:0);

        //将数据传给页面
        model.addAttribute("typeList",typeList);
        model.addAttribute("userBlogInfos",userBlogInfos);
        model.addAttribute("pageCount",pageCount);
        return "index";
    }

thymleaf

      <div class="ui bottom attached segment" th:if="${pageCount}>1">
        <div class="ui middle aligned two column grid">
          <div class="column">
            <a href="#" th:href="@{/index/{pageNum}(pageNum=${pageNum}-1)}"  th:unless="${pageNum==1}" class="ui mini teal basic button">上一页</a>
          </div>
          <div class="right aligned column">
            <a href="#" th:href="@{/index/{pageNum}(pageNum=${pageNum}+1)}"  th:unless="${pageNum==pageCount}" class="ui mini teal basic button">下一页</a>
          </div>
        </div>
      </div>
发布了71 篇原创文章 · 获赞 0 · 访问量 1553

猜你喜欢

转载自blog.csdn.net/qq_42977003/article/details/104801098