簡體   English   中英

無法在Spring Boot中使用Crud存儲庫獲取Redis的結果?

[英]Unable to get result from the Redis using Crud Repository in Spring Boot?

我正在開發Spring Boot + Redis示例。 我從鏈接中獲取了參考: 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;
}

StudentRepository.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)

在此輸入圖像描述

我自己能夠找出以下問題的答案。

您只需要在字段級別放置@Index注釋即可。 @Index注釋標記屬性以及索引,該索引使用Redis {@literal SET}來跟蹤具有匹配值的對象。

@Indexed
private String name;

@Ref: https ://scalegrid.io/blog/introduction-to-redis-data-structures-hashes/

1)如何查看存儲在redis中的實際數據?

redis 127.0.0.1:6379> hgetall Student:1
 1) "_class"
 2) "com.baeldung.spring.data.redis.model.Student"
 3) "id"
 4) "1"
 5) "name"
 6) "John Doe"
 7) "gender"
 8) "MALE"
 9) "grade._class"
10) "java.lang.Integer"
11) "grade"
12) "1"

redis 127.0.0.1:6379> hgetall Student:2
 1) "_class"
 2) "com.baeldung.spring.data.redis.model.Student"
 3) "id"
 4) "2"
 5) "name"
 6) "Michael Harford"
 7) "gender"
 8) "MALE"
 9) "grade._class"
10) "java.lang.Integer"
11) "grade"
12) "2"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM