tkmybatis 分页
时间: 2023-11-21 09:57:25 浏览: 97
以下是使用TkMybatis和PageHelper实现分页查询的步骤:
1.在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
```
2.在application.properties文件中添加以下配置:
```properties
分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
```
3.在Mapper接口中添加分页查询方法:
```java
List<User> selectByPage(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize);
```
4.在Mapper.xml文件中编写分页查询SQL语句:
```xml
<select id="selectByPage" resultType="User">
select * from user
</select>
```
5.在Service层中调用Mapper接口的分页查询方法:
```java
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectByPage(pageNum, pageSize);
PageInfo<User> pageInfo = new PageInfo<>(userList);
return pageInfo;
```
6.在Controller层中接收前端传来的pageNum和pageSize参数,并调用Service层的分页查询方法:
```java
@GetMapping("/users")
public PageInfo<User> getUsers(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
return userService.getUsers(pageNum, pageSize);
}
```
阅读全文