Spring Data Redis(Repositories-Queries and Query Methods)

来源:互联网 发布:怎么投诉淘宝店铺侵权 编辑:程序博客网 时间:2024/05/22 13:40

Queries and Query Methods

查询方法允许通过方法名字自动推导出简单的finder 查询。

Example 19. Sample Repository finder Method

public interface PersonRepository extends CrudRepository<Person, String> {  List<Person> findByFirstname(String firstname);}
请确保在finder 方法中使用的属性都建立了索引。Redis 仓储支持的查询方法,只能查询实体和拥有分页的实体集合。

Using derived query methods might not always be sufficient to model the queries to execute. RedisCallback offers more control over the actual matching of index structures or even custom added ones. All it takes is providing a RedisCallback that returns a single or Iterable set of id values.
使用推导查询方法并不是总能满足要查询的模型。RedisCallback 提供了更多的控制权,在匹配索引结构或自定义模式方面。它所需要的只是提供一个RedisCallback ,RedisCallback 返回一个单例或迭代的id 值集。

Example 20. Sample finder using RedisCallback

String user = //...List<RedisSession> sessionsByUser = template.find(new RedisCallback<Set<byte[]>>() {  public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {    return connection      .sMembers("sessions:securityContext.authentication.principal.username:" + user);  }}, RedisSession.class);

下面是一些Redis 支持的关键字,以及包含关键字的方法应该转变成的样子。

这里写图片描述

阅读全文
0 0
原创粉丝点击