我已经使用Redis配置了Spring Data JPA,并使用RedisRepositories
和find()
,findAll()
等提供的方法。所有这些方法似乎都可以正常工作,但是我无法像这样写我的自定义方法。
RedisEntity findByGenderAndGrade(字符串性别,字符串等级);
RedisEntity是一个简单的POJO Entity类。如果您需要更多信息,请在邮件中告诉我。
以下是我的实体:
@Data
@RedisHash("test1")
public class RedisEntity implements Serializable {
@Id
@GeneratedValue
private String id;
private String name;
private String gender;
private Integer grade;
}
存储库:
@Repository
public interface TestRepository extends JpaRepository<RedisEntity, String> {
List<RedisEntity> findAllByGender(String gender);
List<RedisEntity> findAllByGrade(Integer grade);
}
服务/控制器:
@Override
public List<RedisEntity> getById(String id) {
return testRepository.findById(id); //returns data perfectly.
}
@Override
public List<RedisEntity> getAllByGender(String gender) {
return testRepository.findAllByGender(gender); //returns []
}
@Override
public void saveEntity(RedisEntity redisEntity) {
testRepository.save(redisEntity); // saves it in redis perfectly.
}
另外, findByGender和findAllByGender都给出[],尽管我可以在我的Redis数据库中看到数据并将其保存。
按照FrançoisDupire的要求,
@Configuration
public class RedisConfig {
@Autowired
private DeploymentProperties deploymentProperties;
private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);
@Bean
JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
redisStandaloneConfiguration.setPassword(RedisPassword.of("root"));
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
}
我还引用了这篇文章:Baeldung article on Spring data redis
答案 0 :(得分:0)
我们有Spring Data Redis库,它提供了编写自定义方法的范围。附加示例代码。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>
实体定义
@Data
@RedisHash("EmployeeDetails")
public class RedisEntity {
@Id
private String employeeId;
private String firstName;
private String lastName;
private String gender;
private String grade;
}
存储库定义
@Repository
public interface RedisEntityRepository extends CrudRepository<RedisEntity, String>{
List<RedisEntity> findAllByGenderAndGrade(String gender, String grade);
}
实施
@Component
public class RedisEntityImpl implements RedisEntityService {
@Autowired
private RedisEntityRepository redisEntityRepository;
@Override
public List<RedisEntity> getAllByGenderAndGrade(String gender, String grade) {
return redisEntityRepository.findAllByGenderAndGrade(gender,grade);
}
}
属性
spring.cache.type = redis
spring.redis.host = localhost
spring.redis.port = 6379
答案 1 :(得分:0)
正如@JoshJ 所提到的,并由我自己和其他人验证, 问题的解决方法是:
添加@Indexed 注释 到所有需要与所有发现一起使用的列/字段。
@Data
@RedisHash("EmployeeDetails")
public class RedisEntity {
@Id
private String employeeId;
private String firstName;
private String lastName;
@Indexed
private String gender;
@Indexed
private String grade;
}