mybatis-plus page 的方法
时间: 2023-04-23 21:05:23 浏览: 98
Mybatis-Plus提供了Page类来实现分页查询,具体方法如下:
1. 创建Page对象
Page<T> page = new Page<>(currentPage, pageSize);
其中currentPage表示当前页码,pageSize表示每页显示的记录数。
2. 调用selectPage方法进行分页查询
IPage<T> iPage = mapper.selectPage(page, queryWrapper);
其中mapper表示Mybatis的Mapper接口,queryWrapper表示查询条件。
3. 获取分页结果
List<T> records = iPage.getRecords(); // 当前页的记录列表
long total = iPage.getTotal(); // 总记录数
long pages = iPage.getPages(); // 总页数
通过以上三步,就可以实现Mybatis-Plus的分页查询了。
相关问题
mybatis-plus page
MyBatis-Plus是MyBatis的增强工具,在分页方面也提供了更加便捷的操作。MyBatis-Plus的分页查询使用PageHelper类实现,只需要将Page对象作为参数传入查询方法中即可。
以下是使用MyBatis-Plus进行分页查询的示例代码:
```java
// 创建分页对象
Page<User> page = new Page<>(pageNum, pageSize);
// 执行分页查询
IPage<User> userPage = userDao.selectPage(page, new QueryWrapper<User>());
// 获取分页查询结果
List<User> userList = userPage.getRecords();
long total = userPage.getTotal();
```
其中,`userDao`是MyBatis的Mapper接口,`selectPage`方法是MyBatis-Plus提供的分页查询方法,`new QueryWrapper<User>()`则是MyBatis-Plus提供的查询条件封装类。在查询结果中,`userList`是当前页的数据列表,`total`则是总记录数。
需要注意的是,分页查询方法需要在Mapper接口中定义,如下所示:
```java
public interface UserDao extends BaseMapper<User> {
IPage<User> selectUserPage(Page<User> page, @Param("ew") QueryWrapper<User> wrapper);
}
```
在XML文件中,需要使用`<select>`标签定义分页查询方法,如下所示:
```xml
<select id="selectUserPage" parameterType="com.baomidou.mybatisplus.extension.plugins.pagination.Page" resultMap="userMap">
select * from user
<where>
<if test="ew!=null">
<if test="ew.sqlSegment != null and ew.sqlSegment != ''">
${ew.sqlSegment}
</if>
</if>
</where>
</select>
```
其中,`parameterType`指定了分页对象的类型,`resultMap`则是查询结果的映射。在SQL语句中,需要使用`<if>`标签判断查询条件是否存在,使用`${ew.sqlSegment}`将查询条件拼接到SQL语句中。
mybatis-plus方法大全
MyBatis-Plus 是 MyBatis 的增强工具,在 MyBatis 的基础上增加了许多实用的功能,其中包括了许多方法。
以下是 MyBatis-Plus 中常用的方法:
- insert:插入一条记录
- insertBatch:批量插入记录
- insertOrUpdate:插入或更新记录
- deleteById:根据 ID 删除记录
- deleteByMap:根据条件删除记录
- delete:根据 entity 条件,删除记录
- deleteBatchIds:根据 ID 集合批量删除记录
- updateById:根据 ID 修改记录
- update:根据 whereEntity 条件,更新记录
- updateBatchById:根据 ID 集合批量更新记录
- selectById:根据 ID 查询记录
- selectBatchIds:根据 ID 集合批量查询记录
- selectByMap:根据条件查询记录
- selectOne:查询一条记录
- selectList:查询所有记录
- selectPage:分页查询记录
以上是 MyBatis-Plus 中常用的方法,还有一些其他的方法,具体可以参考 MyBatis-Plus 的官方文档。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)