mybatisplus list分页查询
时间: 2023-10-03 15:07:23 浏览: 115
Spring Boot-MybatisPlus表格分页与下拉分页查询.docx
MyBatis Plus provides a convenient way to perform pagination queries using the `Page` class. Here is an example of how to perform a list pagination query with MyBatis Plus:
```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> getUsersWithPagination(int pageNo, int pageSize) {
Page<User> page = new Page<>(pageNo, pageSize);
return userMapper.selectPage(page, null);
}
}
```
In this example, `User` is the entity class mapped to the database table, and `UserMapper` is the MyBatis Plus mapper interface for `User`. The `getUsersWithPagination` method takes the page number (`pageNo`) and page size (`pageSize`) as parameters.
By creating a new `Page` object and passing it to the `selectPage` method of the mapper, MyBatis Plus will handle the pagination query automatically. The result is returned as an `IPage` object, which contains the paginated data along with other useful information such as total records and current page number.
You can then use the `IPage` object to retrieve the paginated data and display it as needed.
阅读全文