Spring Data Redis(Repositories-Queries and Query Methods)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值