pom.xml文件用到的包

  1. <!-- 引入 thymeleaf 模板依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>
  6. <!--要使用thymeleaf的layout需要引入此包-->
  7. <dependency>
  8. <groupId>nz.net.ultraq.thymeleaf</groupId>
  9. <artifactId>thymeleaf-layout-dialect</artifactId>
  10. </dependency>
  11. <!--mybatis-->
  12. <dependency>
  13. <groupId>org.mybatis.spring.boot</groupId>
  14. <artifactId>mybatis-spring-boot-starter</artifactId>
  15. <version>1.3.2</version>
  16. </dependency>
  17. <!--pagehelper分页功能 -->
  18. <dependency>
  19. <groupId>com.github.pagehelper</groupId>
  20. <artifactId>pagehelper-spring-boot-starter</artifactId>
  21. <version>1.2.3</version>
  22. </dependency>

Controller 代码

  1. package top.bowen.controller;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.HttpRequest;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.ModelMap;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import top.bowen.pojo.Articles;
  12. import top.bowen.pojo.Users;
  13. import top.bowen.service.ArticleService;
  14. import top.bowen.service.UserService;
  15. import top.bowen.utils.TimeFormatHelper;
  16. import java.text.ParseException;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. @Controller
  21. public class IndexController {
  22. @Autowired
  23. ArticleService articleService;
  24. @Autowired
  25. UserService userService;
  26. @RequestMapping(value = {"/","/home"},method = RequestMethod.GET)
  27. public String index(ModelMap map,@RequestParam(name = "type", required = false,defaultValue = "0") Integer type,
  28. @RequestParam(name = "search_content", required = false) String title,
  29. @RequestParam(name = "page",required = false,defaultValue = "0") Integer page,
  30. @RequestParam(name ="pageSize", required = false,defaultValue = "10")Integer pageSize) throws ParseException {
  31. //文章信息,包括分页信息
  32. PageInfo<Articles> articlesPageInfo = articleService.queryArticlesList(type, title, page, pageSize);
  33. map.addAttribute("pageInfo",articlesPageInfo);
  34. return "home";
  35. }
  36. }

service层代码

关键代码
通过startPage方法实现数据库分页
PageHelper.startPage(page, pageSize);
通过PageInfo获取分页查询后的数据,包括数据库列表数据,和一些分页参数
PageInfo articlesPageInfo = new PageInfo<>(articlesList);

  1. package top.bowen.service.impl;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Propagation;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import org.thymeleaf.util.StringUtils;
  10. import tk.mybatis.mapper.entity.Example;
  11. import top.bowen.mapper.ArticlesMapper;
  12. import top.bowen.pojo.Articles;
  13. import top.bowen.service.ArticleService;
  14. import top.bowen.utils.JsonUtils;
  15. import java.util.List;
  16. @Service
  17. public class ArticlesServiceImpl implements ArticleService {
  18. @Autowired
  19. private ArticlesMapper articlesMapper;
  20. @Override
  21. @Transactional(propagation = Propagation.SUPPORTS)
  22. public PageInfo<Articles> queryArticlesList(Integer type, String title, Integer page, Integer pageSize) {
  23. if(page< 0){
  24. page = 0;
  25. }
  26. if(pageSize < 0){
  27. pageSize = 20;
  28. }
  29. //分页类
  30. PageHelper.startPage(page, pageSize);
  31. Example example = new Example(Articles.class);
  32. Example.Criteria criteria = example.createCriteria();
  33. if (type > 0) {
  34. criteria.andEqualTo("type", type);
  35. }
  36. if (!StringUtils.isEmptyOrWhitespace(title)) {
  37. criteria.andLike("title", "%" + title + "%");
  38. }
  39. criteria.andEqualTo("state", 0);
  40. example.orderBy("createdAt").desc();
  41. List<Articles> articlesList = articlesMapper.selectByExample(example);
  42. //3、获取分页查询后的数据
  43. PageInfo<Articles> articlesPageInfo = new PageInfo<>(articlesList);
  44. //4、封装需要返回的分页实体
  45. return articlesPageInfo;
  46. }
  47. }

view层处理 home.html页面

把分页的视图文件包含到当前页面你想要显示分页的位置
判断pageInfo.pages > 1,是为了在只有一页的情况下不显示

  1. <ul class="pagination" th:if="${pageInfo.pages > 1}" th:include="layout/pagination"></ul>

view层处理 pagination.html页面

分页主逻辑页面,即显示的分页条逻辑

  1. <html xmlns:th="http://www.thymeleaf.org">
  2. <!--显示回到上一页的按钮,当是首页时不可点击状态-->
  3. <li class="page-item disabled" th:if="${pageInfo.isIsFirstPage()}"><span class="page-link">«</span></li>
  4. <!--显示回到上一页的按钮,当不是首页时可点击状态-->
  5. <li class="page-item" th:if="${!pageInfo.isIsFirstPage() && pageInfo.getList().size() > 0}"><a class="page-link" th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath() + '?page='+pageInfo.getPrePage()}" rel="prev">«</a></li>
  6. <!--显示页码,当当前页为选择页时显示激活状态-->
  7. <li th:class="|page-item ${(pageInfo.pageNum eq page) ? 'active': ''} |" th:each="page:${pageInfo.navigatepageNums}">
  8. <span class="page-link" th:if="${pageInfo.pageNum eq page}" th:text="${page}"></span>
  9. <a class="page-link" th:unless="${pageInfo.pageNum eq page}" th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath() + '?page='+page}" th:text="${page}">{{ $page }}</a>
  10. </li>
  11. <!--显示回到下一页的按钮,当不是最后一页时可点击状态-->
  12. <li class="page-item" th:if="${pageInfo.hasNextPage}">
  13. <a class="page-link" th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath() + '?page='+pageInfo.nextPage}" rel="next">»</a>
  14. </li>
  15. <!--显示回到下一页的按钮,当是最后一页时不可点击状态-->
  16. <li class="page-item disabled" th:unless="pageInfo.hasNextPage}"><span class="page-link">»</span></li>