PageHelper
概念
PageHelper是适用于MyBatis框架的一个分页插件,使用方式极为便捷,支持任何复杂的单表、多表分页查询操作。
使用步骤
1.引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
2.配置MyBatis-config.xml
在MyBatis-config.xml中添加< plugins >。
<configuration>
<typeAliases></typeAliases>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<environments>...</environments>
</configuration>
3.应用方式
@RequestMapping("bo")
public String getBooks(HttpSession session, Integer pageNum){//获取小说
PageHelper.startPage(pageNum,6);
if (pageNum==null){
pageNum=1;
}
List<BookHT> books = bookService.getBooks();
session.setAttribute("books",books);
PageInfo<BookHT> bookHTPageInfo = new PageInfo<>(books);
session.setAttribute("pp",bookHTPageInfo);
return "forward:Book.jsp";
}
注意:PageHelper.startPage 必须在开始就定义,定义在查所有前面
4.PageInfo对象
下面属性可直接使用
下面是前端的分页代码,就是下图样式
<ul class="pagination">
<c:forEach begin="1" end="${pp.pages}" var="i">
<li><a href="bo?pageNum=${i}">${i}</a></li>
</c:forEach>
</ul>