更新Redis中的单个哈希字段

是否可以在不需要其他字段的情况下更新 Student 对象的单个字段?假设我想更新成绩字段。


import com.ahmetk.redis.redishll.model.Student;

import org.springframework.data.repository.CrudRepository;

import org.springframework.stereotype.Repository;



@Repository

public interface StudentRepository extends CrudRepository<Student, String> {}



@RedisHash("Student")

@Data

@Getter

@Setter

@AllArgsConstructor

public class Student implements Serializable {


   public enum Gender {

      MALE, FEMALE

   }


   private String id;

   private String name;

   private Gender gender;

   private int grade;


   @Override

   public String toString() {

      return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", gender=" + gender + ", grade=" + grade + '}';

   }

}




    @Test

    public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {

        final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);

        studentRepository.save(student);

        studentRepository.delete(student.getId());

        final Student retrievedStudent = studentRepository.findOne(student.getId());

        assertNull(retrievedStudent);

    }


萧十郎
浏览 356回答 1
1回答

泛舟湖上清波郎朗

你可以看看这个 - https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis.repositories.partial-updates文档中示例中的模板是 RedisKeyValueTemplate 的一个实例@Autowiredprivate RedisKeyValueTemplate redisKVTemplate;redisKVTemplate.update(entity)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java