1.引架包。注意版本問題
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.6</version>
</dependency>
2.在MyBatis 的總體文件中配置插件
放到<environments default="development">之前 <plugins> <!-- PageHelper4.1.6 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="oracle"/> </plugin> </plugins> 插件5.1以后interceptor不同,並且不需要指定數據庫名字 <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> </plugin> </plugins>
3.在service層返回值類型是pageInfo,
4.在實現類中
@Override public PageInfo getall(String classname, String classnum, int pageindex, int size,int[] ids,String classstate) { //封裝模糊查條件 Map map =new HashMap(); map.put("classname",classname); map.put("classnum",classnum); map.put("ids",ids); map.put("classstate",classstate); //使用PageInfo工具實現分頁,1、導架包2、配置mybatis.xml PageHelper.startPage(pageindex,size);//在執行查詢之前設置 List list = classesMapper.getall(map); //將插入的集合存在PageInfo分頁工具中 PageInfo pi = new PageInfo(list); return pi; }
5.獲得其他分頁的信息
System.out.println("總條數="+pi.getTotal()); System.out.println("總頁數="+pi.getPages()); System.out.println("下一頁="+pi.getNextPage()); System.out.println("當前頁碼="+pi.getPageNum()); System.out.println("每頁顯示條數:"+pi.getPageSize());