Step 1: setup Redis Server
1 2 | $ brew install redis $ redis-server |
Step 2: setup SpringBoot Redis Client
Install the dependency
1 2 3 4 5 6 7 8 9 10 | < dependency > < groupId >org.springframework.data</ groupId >. < artifactId >spring-data-redis</ artifactId >. < version >2.3.3.RELEASE</ version > </ dependency > < dependency > < groupId >redis.clients</ groupId > < artifactId >jedis</ artifactId > < version >3.3.0</ version > </ dependency > |
Codes:
RedisConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @Configuration @ComponentScan ( "com.example.demo.redis" ) @EnableRedisRepositories (basePackages = "com.example.demo.redis" ) @PropertySource ( "classpath:application.properties" ) public class RedisConfig { /** * Use the Jedis Redis Client to connect with Redis Server * @return */ @Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(); } /** * For querying data with a custom repository. * @return */ @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; } } |
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Data @AllArgsConstructor @NoArgsConstructor @RedisHash ( "Student" ) public class Student implements Serializable { public enum Gender { MALE, FEMALE } private String id; private String name; private Gender gender; private int grade; } |
StudentRepository.java
1 2 3 | @Repository public interface StudentRepository extends CrudRepository<Student, String> { } |
StudentController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | @RestController public class StudentController { @Autowired StudentRepository studentRepository; @GetMapping ( "/saveOneStudent" ) public void saveOneStudent() { Student student = new Student( "Eng2015001" , "John Doe" , Student.Gender.MALE, 1 ); studentRepository.save(student); } @GetMapping ( "/getOneStudent" ) public Student getOneStudent() { Student retrievedStudent = studentRepository.findById( "Eng2015001" ).get(); return retrievedStudent; } @GetMapping ( "/updateOneStudent" ) public Student updateOneStudent() { Student retrievedStudent = studentRepository.findById( "Eng2015001" ).get(); retrievedStudent.setName( "Richard Watson" ); studentRepository.save(retrievedStudent); return retrievedStudent; } @GetMapping ( "/deleteOneStudent" ) public void deleteOneStudent() { studentRepository.deleteById( "Eng2015001" ); } @GetMapping ( "/findAllStudents" ) public List<Student> findAllStudents() { Student engStudent = new Student( "Eng2015001" , "John Doe" , Student.Gender.MALE, 1 ); Student medStudent = new Student( "Med2015001" , "Gareth Houston" , Student.Gender.MALE, 2 ); studentRepository.save(engStudent); studentRepository.save(medStudent); List<Student> students = new ArrayList<>(); studentRepository.findAll().forEach(students::add); return students; } } |
Test with Postman
Test with redis-cli
1 2 | $ redis-cli $ HGETALL Student:Eng2015001 |