依赖
<!-- 分页查询 -->
<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) {
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){
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>