Mybatis-Plus实现分页的核心代码
时间: 2023-05-28 21:07:53 浏览: 134
Mybatis-Plus实现分页的核心代码如下:
1. 在Mapper接口中添加分页方法:
```java
List<User> selectUserList(Page<User> page, @Param("name") String name);
```
2. 在Mapper.xml中编写SQL语句:
```xml
<select id="selectUserList" resultType="User">
SELECT * FROM user WHERE name LIKE CONCAT('%', #{name}, '%')
LIMIT #{page.offset}, #{page.size}
</select>
```
3. 在Service层调用分页方法:
```java
public IPage<User> getUserList(Page<User> page, String name) {
return userMapper.selectUserList(page, name);
}
```
4. 在Controller层获取分页数据并返回给前端:
```java
@GetMapping("/user")
public Result<List<User>> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String name) {
Page<User> page = new Page<>(pageNum, pageSize);
IPage<User> userPage = userService.getUserList(page, name);
return Result.success(userPage.getRecords(), userPage.getTotal());
}
```
其中,Page是Mybatis-Plus提供的分页类,IPage是分页查询结果的封装类,可以获取分页数据以及总记录数。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)