spring整合mybatis分页
时间: 2023-11-04 22:53:15 浏览: 41
在Spring中整合MyBatis分页需要以下步骤:
1. 在pom.xml文件中添加PageHelper的依赖:
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
```
2. 在application.properties文件中配置分页插件的相关属性:
```properties
# 分页插件配置
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
```
3. 在Controller中编写分页方法:
```java
@RequestMapping("/get/page/{pageNo}")
public String getPage(@PathVariable("pageNo") Integer pageNo, Model model) {
// 调用Service层的方法获取分页数据
PageInfo<Emp> pageInfo = empService.getPageInfo(pageNo);
// 将分页结果存入模型中
model.addAttribute("pageInfo", pageInfo);
// 返回视图名称
return "emp-page";
}
```
4. 在页面上添加分页的超链接,用于跳转到不同页码的分页数据:
```html
<a th:href="@{/get/page/1}">显示分页数据</a>
```
通过以上步骤,你就可以在Spring中成功整合MyBatis分页功能了。注意确保你已经正确配置了数据源和MyBatis的相关配置文件。