pagehelper如何使用
时间: 2023-04-09 13:04:49 浏览: 114
PageHelper 是一个开源的 MyBatis 分页插件,可以帮助开发者快速实现分页功能。使用 PageHelper 非常简单,只需要在 MyBatis 的配置文件中配置插件即可。具体步骤如下:
1. 在 pom.xml 文件中添加依赖:
```
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
```
2. 在 MyBatis 的配置文件中添加插件:
```
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
<property name="reasonable" value="true"/>
<property name="supportMethodsArguments" value="true"/>
<property name="params" value="count=countSql"/>
</plugin>
</plugins>
```
其中,`helperDialect` 属性指定了数据库方言,`reasonable` 属性指定了是否启用合理化查询,`supportMethodsArguments` 属性指定了是否支持方法参数,`params` 属性指定了参数别名。
3. 在需要分页的方法中调用 PageHelper.startPage 方法:
```
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userDao.getUserList();
PageInfo<User> pageInfo = new PageInfo<>(userList);
```
其中,`pageNum` 参数指定了当前页码,`pageSize` 参数指定了每页显示的记录数,`userDao.getUserList()` 方法返回了当前页的数据列表,`PageInfo` 类封装了分页信息。
以上就是使用 PageHelper 实现分页的基本步骤。