mybatisplus 分页demo
时间: 2023-07-17 16:07:17 浏览: 55
当使用 MyBatis-Plus 进行分页查询时,你可以按照以下步骤进行操作:
1. 首先,确保你已经引入了 MyBatis-Plus 的依赖。可以在项目的 `pom.xml` 文件中添加以下依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
```
2. 在你的实体类中添加相关注解,例如 `@TableName` 注解来指定表名,`@TableId` 注解来指定主键:
```java
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableId;
@TableName("your_table_name")
public class YourEntity {
@TableId("id")
private Long id;
// 其他属性...
// getter 和 setter 方法...
}
```
3. 编写 Mapper 接口继承 `BaseMapper` 接口,并且在接口上添加 `@Mapper` 注解:
```java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface YourMapper extends BaseMapper<YourEntity> {
}
```
4. 在你的 Service 类中注入 `YourMapper`,然后可以使用 `Page` 类进行分页查询:
```java
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class YourService {
@Autowired
private YourMapper yourMapper;
public Page<YourEntity> getPageData(int pageNum, int pageSize) {
Page<YourEntity> page = new Page<>(pageNum, pageSize);
return yourMapper.selectPage(page, null);
}
}
```
在上述示例中,`selectPage` 方法会返回一个分页对象 `Page<YourEntity>`,你可以从中获取分页信息和查询结果。
这就是 MyBatis-Plus 的分页查询示例。希望对你有所帮助!如有其他问题,请随时提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)