Spring 4.3 集成 Mybatis-Plus 2.1.9 实现分页查询
Spring 4.3 集成 Mybatis-Plus 2.1.9 实现分页查询
使用分页查询可以按照以下步骤:
- 在
pom.xml文件中添加 Mybatis-Plus 的依赖项:
<!-- Mybatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.9</version>
</dependency>
- 在 Mapper 接口中添加方法,使用 Mybatis-Plus 提供的分页查询方法:
public interface UserMapper extends BaseMapper<User> {
/**
* 分页查询用户
*
* @param page 分页参数
* @return 分页结果集
*/
IPage<User> selectUserPage(Page<User> page);
}
- 在 Service 中调用 Mapper 的分页查询方法:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public IPage<User> getUserList(Page<User> page) {
return userMapper.selectUserPage(page);
}
}
- 在 Controller 中调用 Service 的分页查询方法,返回分页结果集:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public Result<List<User>> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
IPage<User> userPage = userService.getUserList(page);
return Result.success(userPage.getRecords(), userPage.getTotal());
}
}
以上就是使用 Mybatis-Plus 实现分页查询的步骤。
原文地址: https://www.cveoy.top/t/topic/omu9 著作权归作者所有。请勿转载和采集!