Mybatis分页插件PageHelper使用教程(图文详细版)
1.配置
小编的项目是springBoot项目,所以首先在pom.xml文件中引入依赖。
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
添加这段代码后,idea会自动帮你下载jar包。
2.后台代码
controller类
@Controller
public class IndexController {
@Autowired
private UserService us;
@Autowired
private QuestionDTOService qdtos;
@Autowired
private HttpServletRequest request;
@RequestMapping("/")
public String index(Model model,@RequestParam(defaultValue = "1") Integer pageNum){
PageHelper.startPage(pageNum,20);// pageNum:当前页码数,第一次进来时默认为1(首页)
List<QuestionDTO> list = qdtos.selectQuestionDTOList();//list:页面要展示的数据的集合
PageInfo<QuestionDTO> pageInfo = new PageInfo<QuestionDTO>(list);//pageInfo:将分页数据和显示的数据封装到PageInfo当中
model.addAttribute("pageInfo",pageInfo);//将封装好的数据返回到前台页面
return "index";
}
html页面
<ul class="pagination">
<li class="page-item" th:if="${pageInfo.pageNum!=1}"><a class="page-link" th:href="@{/(pageNum=1)}">首页</a></li>
<li class="page-item" th:if="${pageInfo.pageNum!=1}"><a class="page-link" th:href="@{/(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:${pageInfo.pages})}">上一页</a></li>
<li class="page-item"><a class="page-link"><span th:text="第+${pageInfo.pageNum}+页"></span></a></li>
<li class="page-item"><a class="page-link"><span th:text="共+${pageInfo.pages}+页"></span></a></li>
<li class="page-item" th:if="${pageInfo.pageNum != pageInfo.pages}"><a class="page-link" th:href="@{/(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a></li>
<li class="page-item" th:if="${pageInfo.pageNum != pageInfo.pages}"><a class="page-link" th:href="@{/(pageNum=${pageInfo.pages})}">尾页</a></li>
</ul>