简体   繁体   English

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

[英]How can I search a redis hash table for entities that have a particular values using spring data?

I have a RedisHash table that I've model in spring data like this:我有一个 RedisHash 表,我在 spring 数据中建模,如下所示:

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

I have an EntityRepository like this:我有一个这样的 EntityRepository:

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

I then have an EntityService like this:然后我有一个这样的 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
  }

How can I search redis for all Entities that have a certain name / have a certain status?如何在 redis 中搜索具有特定名称/具有特定状态的所有实体?

I followed the documentation here and was able to solve my problem.我遵循了此处的文档并能够解决我的问题。 I added the QueryByExampleExecutor interface to my repository like this:我将 QueryByExampleExecutor 接口添加到我的存储库中,如下所示:

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

Using the Example class I implemented getAllByName like this:使用 Example 类,我像这样实现了 getAllByName:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过多个可能的值查找实体列表(使用或在Spring Data中) - How can I find a list of entities by multiple possible values (Using or in Spring Data) 如何在Jtable中搜索特定数据? - How can I search particular data in Jtable? 使用 Spring 引导,如何在新表上创建 2 个实体之间的关系并为其提供额外的列? - Using Spring Boot, how can I create the relationship between 2 entities on a new table and give it extra columns? 我可以对 Redis 值执行正则表达式搜索吗? - Can I perform a regex search on Redis values? 如何使用 Hibernate &amp; Spring Data JPA 正确注释两个实体之间的这种关系? - How can I properly annotate this relationship between two entities using Hibernate & Spring Data JPA? 如何使用Spring Data通过哈希查询DynamoDB表 - How to query DynamoDB table by hash using Spring Data 如何从Hibernate中的表中获取多个实体值? - How can I get multiple entities values from a table in Hibernate? 如何使用 spring 数据 r2dbc 验证实体? - How can I validate entities with spring data r2dbc? 如果我有不相关的实体,如何在Spring Boot中通过多个参数实现搜索? - How to implement a search by multiple params in spring boot if I have unrelated entities? Spring Roo:如何基于多个支持实体编写自定义搜索页面? - Spring Roo: how can I code a customized search page based on several backing entities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM