mybatis分页 mybatis- plus分页
时间: 2024-04-07 14:26:29 浏览: 33
MyBatis是一个优秀的持久层框架,而MyBatis-Plus是在MyBatis基础上进行了扩展和增强的框架。它们都提供了分页功能来方便处理大量数据的查询操作。
在MyBatis中实现分页可以通过使用插件或者自定义SQL语句来实现,而MyBatis-Plus分页,你可以按照以下步骤进行操作:
1. 在你的实体类中添加分页参数,例如`current`表示当前页码,`size`表示每页显示的记录数。
2. 在你的Mapper接口中继承`BaseMapper`接口,该接口提供了一些常用的CRUD方法,包括分页查询方法。
3. 在你的Service层中调用分页查询方法,传入分页参数和查询条件。
4. 在Controller层中接收分页结果,并返回给前端展示。
下面是一个示例代码:
```java
// 实体类
public class User {
private Long id;
private String name;
// 省略getter和setter方法
}
// Mapper接口
public interface UserMapper extends BaseMapper<User> {
// 省略其他方法
}
// Service层
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public IPage<User> getUserList(int current, int size) {
Page<User> page = new Page<>(current, size);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// 设置查询条件
// queryWrapper.eq("name", "张三");
return userMapper.selectPage(page, queryWrapper);
}
}
// Controller层
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public IPage<User> getUserList(@RequestParam(defaultValue = "1") int current,
@RequestParam(defaultValue = "10") int size) {
return userService.getUserList(current, size);
}
}
```
以上示例中,`getUserList`方法实现了分页查询功能,通过调用`selectPage`方法实现分页查询,并将查询结果封装到`IPage`对象中返回给前端。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)