轻松解决分页问题 老板再也不会觉得你工作效率慢
使用PageHelper需要引入jar包。<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
然后在在mybatis-config.xml中添加<!--plugins在settings之后,environments之前-->
<plugins>
<!-- PageHelper4.1.1 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/><!--数据库方言-->
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="false"/>
</plugin>
</plugins>
@RequestMapping("list.html")
public ModelAndView get(Integer user_id, @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex){
System.out.println(pageIndex);
PageHelper.startPage(pageIndex, 3);//对紧随其后第一个sql语句有效
List<BlogRiJi> riJiList = blogRiJiService.getRiJiList(user_id);
PageInfo<BlogRiJi> page = new PageInfo<>(riJiList);//把查询到的集合放入Page中
return new ModelAndView("riji/list", "page", page);
}
然后在页面调用即可
下面给出PageInfo类的属性
private static final long serialVersionUID = 1L;
private int pageNum;
private int pageSize;
private int size;
private String orderBy;
private int startRow;
private int endRow;
private long total;
private int pages;
private List<T> list;
private int prePage;
private int nextPage;
private boolean isFirstPage;
private boolean isLastPage;
private boolean hasPreviousPage;
private boolean hasNextPage;
private int navigatePages;
private int[] navigatepageNums;
private int navigateFirstPage;
private int navigateLastPage;