java相关:Spring集成PageHelper的简单用法示例
发布于 2020-12-14|
复制链接
摘记: 1、Maven依赖,注意使用PageHelper时的版本必须与Mybatis版本对应
```xml
org.mybatis
mybatis
..
1、Maven依赖,注意使用PageHelper时的版本必须与Mybatis版本对应
```xml
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
```xml
pages会查询最后一页 -->
pages会返回空数据 -->
```
3、在配置Spring配置文件中,配置Mybatis的SqlSessionFactory时,需要把mybatis-config.xml添加到属性中
```xml
```
4、使用Mybatis的mapper接口进行查询,在查询时,需要使用PageHelper.startPage方法
```java
@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是否存在下一页等。