Spring data Pageable and LIMIT/OFFSET
public class OffsetLimitPageable extends PageRequest {
private int offset;
public OffsetLimitPageable(int offset, int limit){
super(offset,limit);
this.offset=offset;
}
@Override
public long getOffset(){
return this.offset;
}
}
And example
Page<WashComment> washComments = washCommentRepository.findWashCommentByWashId_CarWashIdOrderByDateDesc(carWash, new OffsetLimitPageable(offsetNumberRepresentation,
limitNumberRepresentation > Config.getMaxLimitAmount() ? Config.getMaxLimitAmount() : limitNumberRepresentation));
Let me know if this is what you want
Pagebale implementations do use limit
and offset
to create pagination. The page
value in the constructor is used to generate the offset value in the AbstractPageRequest
class getOffset
method:
public int getOffset() {
return this.page * this.size;
}
If you want to only use limit
and offset
and discard the page
parameter from the mix, take a look at the Spring Data documentation on web support, particularly the part about overriding the default configuration. You could create your own implementation of Pageable
that takes limit
and offset
as constructor arguments and the implement your own HandlerMethodArgumentResolver
to replace the standard PageRequest
resolving. Quick-and-dirty example:
Pageable implementation
public class BetterPageRequest implements Pageable {
public BetterPageRequest(int limit, int offset){
this.limit = limit;
this.offset = offset;
}
// Other method implementations
}
HandlerMethodArgumentResolver implementation
public class BetterPageableResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter){
return Pageable.class.equals(parameter.getParameterType());
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, WebDataBinderFactory factory){
Map<String,String[]> params = request.getParameterMap();
return new BetterPageRequest(params.get('limit')[0], params.get('offset')[0]);
}
}