[英]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.