1、Maven依赖,注意使用PageHelper时的版本必须与Mybatis版本对应
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.3
com.github.pagehelper
pagehelper
4.1.4
2、需要在Mybatis的配置信息中使用PageHelper插件,mybatis-config.xml
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
3、在配置Spring配置文件中,配置Mybatis的SqlSessionFactory时,需要把mybatis-config.xml添加到属性中
4、使用Mybatis的mapper接口进行查询,在查询时,需要使用PageHelper.startPage方法
@Test
public void testSelectAll() {
Page page = PageHelper.startPage(1, 3);
//selectAll查询出的List即为上面定义的page
doctorMapper.selectAll();
//注意:
//使用PageHelper.startPage只是针对接下来的一条查询语句,
//如果又查询了一次数据,则还需要使用一次PageHelper.startPage
logger.info("获取所有Doctor信息,获得记录数:{}", page.size());
logger.info("获取所有Doctor信息,获得记录:{}", page);
//使用PageInfo封装
PageInfo info = new PageInfo(page);
logger.info("info.getPages:{}",info.getPages());
}
5、Page对象继承了ArrayList,因此在使用了PageHelper.startPage之后,Page即为查询到的数据,并且在Page中还额外封装了pageNum,pageSize等属性,还可以使用PageInfo封装Page,PageInfo中有更多的分页属性,例如isFirstPage是否为首页、isLastPage是否为末尾、hasNextPage是否存在下一页等。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。