mybatisplus 分页
时间: 2023-09-03 20:15:51 浏览: 89
MyBatis-Plus是一个MyBatis的增强工具,它提供了一些常见的功能,其中包括分页查询。下面是使用MyBatis-Plus进行分页查询的步骤:
1.引入MyBatis-Plus的依赖
Maven:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
```
Gradle:
```
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '${mybatis-plus.version}'
```
2.在mapper接口中继承BaseMapper<T>,其中T为实体类
```
public interface UserMapper extends BaseMapper<User> {
//...
}
```
3.使用Page对象进行分页查询
```
Page<User> page = new Page<>(1, 10); //第1页,每页10条数据
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("gender", "male"); //查询条件
IPage<User> userPage = userMapper.selectPage(page, wrapper);
List<User> userList = userPage.getRecords(); //查询结果
long total = userPage.getTotal(); //总记录数
```
其中,Page对象表示分页信息,包括当前页码和每页条数;QueryWrapper对象表示查询条件;selectPage方法进行分页查询,返回IPage对象,其中包含查询结果和总记录数。getRecords方法获取查询结果,getTotal方法获取总记录数。
这就是使用MyBatis-Plus进行分页查询的基本流程。
阅读全文
相关推荐









