java—如何使用spring数据在redis哈希表中搜索具有特定值的实体?

o3imoua4  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(338)

我有一个在spring数据中建模的哈希表,如下所示:

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

我有这样一个完整的描述:

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

我有这样一个实体服务:

@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中搜索具有特定名称/状态的所有实体?

ia2d9nvy

ia2d9nvy1#

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

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

使用示例类,我实现了如下getallbyname:

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

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

相关问题