slyak / spring-data-jpa-extra

spring data jpa with template dynamic query (eg: freemarker,velocity etc.) like mybatis. The master branch already support springboot2+ spring5+
Apache License 2.0
400 stars 116 forks source link

能否优化分页参数,任何实现了Pageable接口的类都可以分页 #18

Closed 1013785085 closed 6 years ago

1013785085 commented 6 years ago

这样做可以将分页参数和其它参数合并为一个参数,比如Page findTest(PageParams params); PageParams 是一个map,而且是个分页参数 `public class GenericJpaQueryMethod extends JpaQueryMethod {

public GenericJpaQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory, QueryExtractor extractor) {
    super(method, metadata, factory, extractor);
}

@Override
protected JpaParameters createParameters(Method method) {
    JpaParameters parameters =new JpaParameters(method);
    try {
        if(!parameters.hasPageableParameter()){
            int size = parameters.getNumberOfParameters();
            for(int i=0;i<size;i++){
                Parameter parameter = parameters.getParameter(i);
                if(Pageable.class.isAssignableFrom(parameter.getType())){
                    Field field = Parameters.class.getDeclaredField("pageableIndex");
                    field.setAccessible(true);
                    Field modifiersField = Field.class.getDeclaredField("modifiers");
                    modifiersField.setAccessible(true);
                    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                    field.set(parameters, i);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return  parameters;
}

} TemplateQueryLookupStrategy类 return new FreemarkerTemplateQuery(new GenericJpaQueryMethod(method, metadata, factory, extractor), entityManager);`

stormning commented 6 years ago

query和pageable分开比较好吧,这个建议再议

chaosknn commented 6 years ago

query和pageable分开比较好吧

1013785085 commented 6 years ago

已经代码下载,做了定制化修改,因为没有理解分开的好处,个人更喜欢能减少代码量