我正在开发Sping Boot + Redisexample。我引用了link的一段话:https://www.baeldung.com/spring-data-redis-tutorial。在这个例子中,我开发了存储库方法Student findByNameAndGender(String name, Gender gender);
,甚至Student findByName(String name);
,但我在这两种情况下都没有得到任何结果。
有什么能帮忙的吗?Redis查询-
redis 127.0.0.1:6379> KEYS *
1) "Student"
2) "Student:bb4df14a-7f42-4fc3-b608-fc4b7d45109e"
3) "Student:69affaa4-e56c-49e3-9ef4-1cd7509d299b"
redis 127.0.0.1:6379>
字符串
Student.java
@Data
@AllArgsConstructor
@Builder
@NoArgsConstructor
@RedisHash("Student")
public class Student {
public enum Gender {
MALE, FEMALE
}
private String id;
private String name;
private Gender gender;
private int grade;
}
型
学生仓库.java
@Repository
public interface StudentRepository extends CrudRepository<Student, String>{
Student findByNameAndGender(String name, Gender gender);
}
型
RedisConfig.java
@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;
}
}
型
SpringDataRedisEugenpApplication.java
@SpringBootApplication
public class SpringDataRedisEugenpApplication implements CommandLineRunner{
@Autowired
private StudentRepository repository;
public static void main(String[] args) {
SpringApplication.run(SpringDataRedisEugenpApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
repository.save(Student.builder().name("John").gender(Student.Gender.MALE).grade(1).build());
repository.save(Student.builder().name("Michael").gender(Student.Gender.MALE).grade(2).build());
Iterable<Student> students = repository.findAll();
for (Student student : students) {
System.out.println("=============================");
System.out.println("Details ? "+student.toString());
}
Student s = repository.findByNameAndGender("John", Student.Gender.MALE);
System.out.println("Student ="+s.toString()); //line-34
}
}
型
错误-
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at com.baeldung.SpringDataRedisEugenpApplication.main(SpringDataRedisEugenpApplication.java:19) [classes/:na]
Caused by: java.lang.NullPointerException: null
at com.baeldung.SpringDataRedisEugenpApplication.run(SpringDataRedisEugenpApplication.java:34) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:792) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
... 5 common frames omitted
型
几个问题-
1)如何查看存储在Redis中的实际数据?
2)如何控制Id从一开始?
edis 127.0.0.1:6379> get Student:1
(error) ERR Operation against a key holding the wrong kind of value
redis 127.0.0.1:6379> get Student:2
(error) ERR Operation against a key holding the wrong kind of value
redis 127.0.0.1:6379>
型
我真的很难理解
redis 127.0.0.1:6379> mget Student:1
1) (nil)
redis 127.0.0.1:6379> mget Student:2
1) (nil)
redis 127.0.0.1:6379> mget Student
1) (nil)
型
x1c 0d1x的数据
1条答案
按热度按时间gfttwv5a1#
我自己能够想出以下问题的答案。
您只需要将
@Index
注解放在字段级别。@Index
注解将属性标记为索引,使用Redis {@literal SET}来跟踪具有匹配值的对象。字符串
@参考:https://scalegrid.io/blog/introduction-to-redis-data-structures-hashes/
1)如何查看存储在Redis中的实际数据?
型