我已经使用 Redis 配置了 Spring Data JPA 并使用了诸如等RedisRepositories
提供的方法。所有这些方法似乎都工作得很好,但我无法编写我的自定义方法。find()
findAll()
RedisEntity findByGenderAndGrade(字符串性别,字符串等级);
RedisEntity 是一个简单的 POJO 实体类。如果您想了解更多信息,请在消息中告诉我。
以下是我的实体:
@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