如何生成Redis Document实体元模型以用于Redis OM EntityStream查询
Redis OM Spring 生成实体元模型(如Person$)的方法
要生成Redis OM Spring所需的实体元模型(比如示例中的Person$),需通过编译时注解处理器实现,具体步骤如下:
1. 引入依赖
确保项目中包含Redis OM Spring核心依赖,以及对应的注解处理器依赖(版本需保持一致):
Maven 配置(pom.xml)
<!-- Redis OM Spring 核心依赖 --> <dependency> <groupId>com.redis.om</groupId> <artifactId>redis-om-spring</artifactId> <version>0.8.0</version> <!-- 替换为当前最新版本 --> </dependency> <!-- 注解处理器,用于生成元模型 --> <dependency> <groupId>com.redis.om</groupId> <artifactId>redis-om-spring-processor</artifactId> <version>0.8.0</version> <scope>provided</scope> </dependency>
Gradle 配置(build.gradle)
dependencies { // Redis OM Spring 核心依赖 implementation 'com.redis.om:redis-om-spring:0.8.0' // 注解处理器 annotationProcessor 'com.redis.om:redis-om-spring-processor:0.8.0' }
2. 标记实体类
给Person实体类添加Redis OM的注解,确保处理器能识别并生成元模型:
import com.redis.om.spring.annotations.Document; import com.redis.om.spring.annotations.Indexed; import lombok.Data; @Document // 标记为Redis JSON文档实体 @Data public class Person { @Indexed // 标记需要索引的字段,元模型会生成对应属性 private String id; @Indexed private int age; private String name; }
3. 触发元模型生成
执行项目编译操作,注解处理器会在编译阶段自动生成元模型类:
- Maven:执行命令
mvn compile - Gradle:执行命令
gradle compileJava
编译完成后,元模型类(如Person$)会生成在以下目录:
- Maven:
target/generated-sources/annotations - Gradle:
build/generated/sources/annotationProcessor/java/main
4. 使用元模型
编译完成后,即可像示例代码一样使用生成的元模型构建查询:
// Find people by age range public Iterable<Person> findByAgeBetween(int minAge, int maxAge) { return entityStream // .of(Person.class) // .filter(Person$.AGE.between(minAge, maxAge)) // .sorted(Person$.AGE, SortOrder.ASC) // .collect(Collectors.toList()); }
内容的提问来源于stack exchange,提问作者efk
相关产品推荐
相关产品推荐

