mybatis-plus 怎么做分页查询
时间: 2023-10-18 16:28:40 浏览: 166
在 MyBatis-Plus 中,可以通过使用 `Page` 对象来实现分页查询。以下是实现分页查询的步骤:
1. 导入相关依赖:确保在项目的 `pom.xml` 文件中添加了 MyBatis-Plus 的依赖。
2. 设置分页参数:创建一个 `Page` 对象,指定当前页码和每页显示的数据条数。
```java
Page<User> page = new Page<>(pageNum, pageSize);
```
3. 执行分页查询:在查询方法中使用 `page` 对象作为参数,MyBatis-Plus 会自动进行分页查询。
```java
IPage<User> userPage = userMapper.selectPage(page, null);
```
4. 获取分页结果:通过 `userPage` 对象可以获取分页查询的结果。
```java
List<User> userList = userPage.getRecords(); // 当前页的数据列表
long total = userPage.getTotal(); // 总记录数
long current = userPage.getCurrent(); // 当前页码
long pages = userPage.getPages(); // 总页数
```
完整的示例代码如下所示:
```java
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public IPage<User> getUserListByPage(int pageNum, int pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
return userMapper.selectPage(page, null);
}
}
```
请根据自己的业务需求和实际情况进行相应的调整和使用。
阅读全文
相关推荐
















