MyBatis-Plus分页插件提供了IPage接口来支持分页查询。下面是一个使用示例:
public interface UserMapper extends BaseMapper<User> {
IPage<User> selectUserPage(IPage<User> page, @Param("username") String username);
}
<select id="selectUserPage" parameterType="com.baomidou.mybatisplus.extension.plugins.pagination.Page">
SELECT *
FROM user
<where>
<if test="username != null and username != ''">
AND username like CONCAT('%', #{username}, '%')
</if>
</where>
</select>
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public IPage<User> selectUserPage(int pageNum, int pageSize, String username) {
Page<User> page = new Page<>(pageNum, pageSize);
return baseMapper.selectUserPage(page, username);
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public IPage<User> getUsers(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam(defaultValue = "") String username) {
return userService.selectUserPage(pageNum, pageSize, username);
}
}
这样就可以通过访问/users
接口来实现分页查询,传入页码和每页数量可以获取对应的分页数据。