我有一个RedisHash表,我已经在Spring数据中进行了如下建模:

@RedisHash("Entity")
@Data
@AllArgsConstructor
public class Entity implements Serializable {
  private String id;
  private String status;
  private String name;
}


我有一个这样的EntityRepository:

@Repository
public interface EntityRepository extends CrudRepository<Entity, String> {}


然后,我有一个这样的EntityService:

@Service
public class EntityService {
  @Autowired
  private EntityRepository entityRepository;

  public List<Entity> getAllByName(String name) {
    // Code that gets all Entities stored in my redis table that have a certain name
  }

  public List<Entity> getAllByStatus(String status) {
    // Code that gets all Entities stored in my redis table that have a certain status
  }


如何在Redis中搜索所有具有特定名称/具有特定状态的实体?

最佳答案

我遵循了文档here并能够解决我的问题。我将QueryByExampleExecutor接口添加到我的存储库中,如下所示:

@Repository
public interface EntityRepository extends CrudRepository<Entity, String>, QueryByExampleExecutor<Entity> {}


使用Example类,我实现了getAllByName,如下所示:

public List<Entity> getAllByName(String name) {
    Entity entity = new Entity();
    entity.setName(name);

    Example<Entity> example = Example.of(entity);
    entityRepository.findAll(example);
    //...
}

10-07 20:40