无法在Spring Boot中使用Crud存储库从Redis获得结果? [英] Unable to get result from the Redis using Crud Repository in Spring Boot?

查看:123
本文介绍了无法在Spring Boot中使用Crud存储库从Redis获得结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 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/引入到redis数据结构哈希/

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"

I am developing Spring Boot + Redis example. I've taken a reference from link : https://www.baeldung.com/spring-data-redis-tutorial. In this example, I developed repository method Student findByNameAndGender(String name, Gender gender); and even the Student findByName(String name);, but I am not getting any result back on both cases.?

Any quick help? Redis query -

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
    }
}

Error -

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

Few Queries -

1) How to see actual data stored in the redis?

2) How we can control Id to be started from one ?

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>

I am really struggling to see

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)

解决方案

I myself able to figure out answers to below questions.

You just need to put the @Index annotation at the field level. @Index annotions marks the property as for indexing which uses Redis {@literal SET} to keep track of for objects with matching values.

@Indexed
private String name;

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

1) How to see actual data stored in the 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"

这篇关于无法在Spring Boot中使用Crud存储库从Redis获得结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆