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());