我想从Spring引导应用程序中使用Redis并检索分页和排序的结果。
Maven配置:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.10.2</version>
<type>jar</type>
</dependency>
基于一个在线示例,我实现了以下存储库:
@Repository
public interface StudentRepository extends PagingAndSortingRepository<Student, String> {
List<Student> findByName(String Name, Pageable page);
List<Student> findByName(String Name);
}
现在我想使用过滤我的学生
studentRepository.findAll(Sort.by("range").ascending());
与学生一起:
@RedisHash("Student")
public class Student implements Serializable {
@Id
private String id;
@Indexed
private String name;
private Gender gender;
private int grade;
无论排序如何,顺序始终相同。经过调试会话后,我了解到原因是:
class RedisQueryEngine extends QueryEngine<RedisKeyValueAdapter, RedisOperationChain, Comparator<?>> {
/**
* Creates new {@link RedisQueryEngine} with defaults.
*/
RedisQueryEngine() {
this(new RedisCriteriaAccessor(), null);
}
/**
* Creates new {@link RedisQueryEngine}.
*
* @param criteriaAccessor
* @param sortAccessor
* @see QueryEngine#QueryEngine(CriteriaAccessor, SortAccessor)
*/
private RedisQueryEngine(CriteriaAccessor<RedisOperationChain> criteriaAccessor,
@Nullable SortAccessor<Comparator<?>> sortAccessor) {
super(criteriaAccessor, sortAccessor);
}
Spring调用的公共构造函数未定义sortAccessor。然后,我的查询在QueryEngine类中使用了这种排序访问器:
public <T> Collection<T> execute(KeyValueQuery<?> query, String keyspace, Class<T> type) {
CRITERIA criteria = this.criteriaAccessor.map(it -> it.resolve(query)).orElse(null);
SORT sort = this.sortAccessor.map(it -> it.resolve(query)).orElse(null);
return execute(criteria, sort, query.getOffset(), query.getRows(), keyspace, type);
}
这是预期的行为吗? Redis可以只分页结果(我测试过了吗),却不能排序(在这种情况下,分页完全没有用)?