Spring data Pageable and LIMIT/OFFSET

前端 未结 2 812
花落未央
花落未央 2020-12-18 23:05

i\'m looking at migrating our traditional jpa/dao solution to Spring Data.

However, one of our front-ends is SmartGWT, and their databound components load data progr

相关标签:
2条回答
  • 2020-12-18 23:07

    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]);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-18 23:09
    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 it what you want

    0 讨论(0)
提交回复
热议问题