推薦好用的分頁外掛PageHelper
阿新 • • 發佈:2018-12-23
輕鬆解決分頁問題 老闆再也不會覺得你工作效率慢
使用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;