mybatis分页插件-pagehelper如何使用
时间: 2023-09-26 12:11:30 浏览: 186
关于使用 MyBatis 分页插件 PageHelper,可以参考以下步骤:
1. 在项目的 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
```
2. 在 MyBatis 配置文件中配置 PageHelper 插件:
```xml
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--数据库类型,mybatis会根据不同的数据库使用不同的分页策略-->
<property name="helperDialect" value="mysql"/>
<!--参数映射,不设置该参数时使用默认值-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
```
3. 在代码中使用 PageHelper.startPage 方法设置分页查询:
```java
// pageNum:当前页码,pageSize:每页显示条数
PageHelper.startPage(pageNum, pageSize);
// 调用 MyBatis 的查询方法,会自动实现分页
List<User> userList = userDao.queryUsers();
```
4. 在页面中显示分页信息和查询结果:
```html
<!-- pageInfo 是 PageHelper 返回的分页信息 -->
<nav>
<ul class="pagination">
<li><a href="${pageInfo.prePage}">上一页</a></li>
<li><a href="${pageInfo.nextPage}">下一页</a></li>
</ul>
</nav>
<!-- userList 是上述查询返回的结果 -->
<table>
<thead>
<tr>
<th>编号</th>
<th>用户名</th>
<th>邮箱</th>
<th>手机号码</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.email}</td>
<td>${user.mobile}</td>
</tr>
</c:forEach>
</tbody>
</table>
```
希望这份简单的使用说明能够帮到你,如果有其它问题,请随时提出。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)