mybatis-plus page
时间: 2023-09-09 09:07:13 浏览: 123
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语句中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)