pom.xml文件用到的包
<!-- 引入 thymeleaf 模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--要使用thymeleaf的layout需要引入此包-->
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--pagehelper分页功能 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
Controller 代码
package top.bowen.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import top.bowen.pojo.Articles;
import top.bowen.pojo.Users;
import top.bowen.service.ArticleService;
import top.bowen.service.UserService;
import top.bowen.utils.TimeFormatHelper;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Controller
public class IndexController {
@Autowired
ArticleService articleService;
@Autowired
UserService userService;
@RequestMapping(value = {"/","/home"},method = RequestMethod.GET)
public String index(ModelMap map,@RequestParam(name = "type", required = false,defaultValue = "0") Integer type,
@RequestParam(name = "search_content", required = false) String title,
@RequestParam(name = "page",required = false,defaultValue = "0") Integer page,
@RequestParam(name ="pageSize", required = false,defaultValue = "10")Integer pageSize) throws ParseException {
//文章信息,包括分页信息
PageInfo<Articles> articlesPageInfo = articleService.queryArticlesList(type, title, page, pageSize);
map.addAttribute("pageInfo",articlesPageInfo);
return "home";
}
}
service层代码
关键代码
通过startPage方法实现数据库分页
PageHelper.startPage(page, pageSize);
通过PageInfo获取分页查询后的数据,包括数据库列表数据,和一些分页参数
PageInfo
package top.bowen.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import top.bowen.mapper.ArticlesMapper;
import top.bowen.pojo.Articles;
import top.bowen.service.ArticleService;
import top.bowen.utils.JsonUtils;
import java.util.List;
@Service
public class ArticlesServiceImpl implements ArticleService {
@Autowired
private ArticlesMapper articlesMapper;
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public PageInfo<Articles> queryArticlesList(Integer type, String title, Integer page, Integer pageSize) {
if(page< 0){
page = 0;
}
if(pageSize < 0){
pageSize = 20;
}
//分页类
PageHelper.startPage(page, pageSize);
Example example = new Example(Articles.class);
Example.Criteria criteria = example.createCriteria();
if (type > 0) {
criteria.andEqualTo("type", type);
}
if (!StringUtils.isEmptyOrWhitespace(title)) {
criteria.andLike("title", "%" + title + "%");
}
criteria.andEqualTo("state", 0);
example.orderBy("createdAt").desc();
List<Articles> articlesList = articlesMapper.selectByExample(example);
//3、获取分页查询后的数据
PageInfo<Articles> articlesPageInfo = new PageInfo<>(articlesList);
//4、封装需要返回的分页实体
return articlesPageInfo;
}
}
view层处理 home.html页面
把分页的视图文件包含到当前页面你想要显示分页的位置
判断pageInfo.pages > 1,是为了在只有一页的情况下不显示
<ul class="pagination" th:if="${pageInfo.pages > 1}" th:include="layout/pagination"></ul>
view层处理 pagination.html页面
分页主逻辑页面,即显示的分页条逻辑
<html xmlns:th="http://www.thymeleaf.org">
<!--显示回到上一页的按钮,当是首页时不可点击状态-->
<li class="page-item disabled" th:if="${pageInfo.isIsFirstPage()}"><span class="page-link">«</span></li>
<!--显示回到上一页的按钮,当不是首页时可点击状态-->
<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>
<!--显示页码,当当前页为选择页时显示激活状态-->
<li th:class="|page-item ${(pageInfo.pageNum eq page) ? 'active': ''} |" th:each="page:${pageInfo.navigatepageNums}">
<span class="page-link" th:if="${pageInfo.pageNum eq page}" th:text="${page}"></span>
<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>
</li>
<!--显示回到下一页的按钮,当不是最后一页时可点击状态-->
<li class="page-item" th:if="${pageInfo.hasNextPage}">
<a class="page-link" th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath() + '?page='+pageInfo.nextPage}" rel="next">»</a>
</li>
<!--显示回到下一页的按钮,当是最后一页时不可点击状态-->
<li class="page-item disabled" th:unless="pageInfo.hasNextPage}"><span class="page-link">»</span></li>