diff --git a/spring-data-redis/README.md b/spring-data-redis/README.md new file mode 100644 index 0000000000..b9b2e5d93d --- /dev/null +++ b/spring-data-redis/README.md @@ -0,0 +1,15 @@ +## Spring Data Redis + +### Relevant Articles: +- [Introduction to Spring Data Redis] + +### Build the Project with Tests Running +``` +mvn clean install +``` + +### Run Tests Directly +``` +mvn test +``` + diff --git a/spring-data-redis/pom.xml b/spring-data-redis/pom.xml new file mode 100644 index 0000000000..98da69934c --- /dev/null +++ b/spring-data-redis/pom.xml @@ -0,0 +1,76 @@ + + 4.0.0 + + org.baeldung + sprint-data-redis + 0.0.1-SNAPSHOT + jar + + + UTF-8 + 4.2.2.RELEASE + 1.6.2.RELEASE + 0.8.0 + + + + + org.springframework.data + spring-data-redis + ${spring-data-redis} + + + + cglib + cglib-nodep + 2.2 + + + + log4j + log4j + 1.2.16 + + + + redis.clients + jedis + 2.5.1 + jar + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework + spring-context + ${spring.version} + + + + junit + junit + 4.12 + test + + + + org.springframework + spring-test + ${spring.version} + test + + + + com.lordofthejars + nosqlunit-redis + ${nosqlunit.version} + + + + diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java new file mode 100644 index 0000000000..a7e75a438a --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java @@ -0,0 +1,24 @@ +package org.baeldung.spring.data.redis.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; + +@Configuration +@ComponentScan("org.baeldung.spring.data.redis") +public class RedisConfig { + + @Bean + JedisConnectionFactory jedisConnectionFactory() { + return new JedisConnectionFactory(); + } + + @Bean + public RedisTemplate redisTemplate() { + final RedisTemplate< String, Object> template = new RedisTemplate(); + template.setConnectionFactory(jedisConnectionFactory()); + return template; + } +} diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java new file mode 100644 index 0000000000..acc96899ce --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java @@ -0,0 +1,59 @@ +package org.baeldung.spring.data.redis.model; + +import java.io.Serializable; + +public class Student implements Serializable { + + public enum Gender { + MALE, FEMALE + } + + private String id; + private String name; + private Gender gender; + private int grade; + + public Student(String id, String name, Gender gender, int grade) { + this.id = id; + this.name = name; + this.gender = gender; + this.grade = grade; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Gender getGender() { + return gender; + } + + public void setGender(Gender gender) { + this.gender = gender; + } + + public int getGrade() { + return grade; + } + + public void setGrade(int grade) { + this.grade = grade; + } + + @Override + public String toString() { + return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", gender=" + gender + ", grade=" + grade + '}'; + } +} \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java new file mode 100644 index 0000000000..6a909ed137 --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java @@ -0,0 +1,19 @@ +package org.baeldung.spring.data.redis.repo; + +import org.baeldung.spring.data.redis.model.Student; +import org.springframework.stereotype.Component; + +import java.util.Map; + +public interface StudentRepository { + + void saveStudent(Student person); + + void updateStudent(Student student); + + Student findStudent(String id); + + Map findAllStudents(); + + void deleteStudent(String id); +} diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java new file mode 100644 index 0000000000..55e6ad5edc --- /dev/null +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java @@ -0,0 +1,37 @@ +package org.baeldung.spring.data.redis.repo; + +import org.baeldung.spring.data.redis.model.Student; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import java.util.Map; + +@Repository +public class StudentRepositoryImpl implements StudentRepository { + + private static final String KEY = "Student"; + + @Autowired + private RedisTemplate redisTemplate; + + public void saveStudent(final Student student) { + redisTemplate.opsForHash().put(KEY, student.getId(), student); + } + + public void updateStudent(final Student student) { + redisTemplate.opsForHash().put(KEY, student.getId(), student); + } + + public Student findStudent(final String id) { + return (Student) redisTemplate.opsForHash().get(KEY, id); + } + + public Map findAllStudents() { + return redisTemplate.opsForHash().entries(KEY); + } + + public void deleteStudent(final String id) { + this.redisTemplate.opsForHash().delete(KEY, id); + } +} diff --git a/spring-data-redis/src/main/resources/logback.xml b/spring-data-redis/src/main/resources/logback.xml new file mode 100644 index 0000000000..215eeede64 --- /dev/null +++ b/spring-data-redis/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-redis/src/main/resources/test.png b/spring-data-redis/src/main/resources/test.png new file mode 100644 index 0000000000..c3b5e80276 Binary files /dev/null and b/spring-data-redis/src/main/resources/test.png differ diff --git a/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java new file mode 100644 index 0000000000..08540abd36 --- /dev/null +++ b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java @@ -0,0 +1,60 @@ +package org.baeldung.spring.data.redis.repo; + +import org.baeldung.spring.data.redis.config.RedisConfig; +import org.baeldung.spring.data.redis.model.Student; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = RedisConfig.class) +public class StudentRepositoryTest { + + @Autowired + private StudentRepository studentRepository; + + @Test + public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepository.saveStudent(student); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); + assertEquals(student.getId(), retrievedStudent.getId()); + } + + @Test + public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepository.saveStudent(student); + student.setName("Richard Watson"); + studentRepository.saveStudent(student); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); + assertEquals(student.getName(), retrievedStudent.getName()); + } + + @Test + public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { + final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2); + studentRepository.saveStudent(engStudent); + studentRepository.saveStudent(medStudent); + final Map retrievedStudent = studentRepository.findAllStudents(); + assertEquals(retrievedStudent.size(), 2); + } + + @Test + public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepository.saveStudent(student); + studentRepository.deleteStudent(student.getId()); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); + assertNull(retrievedStudent); + } +} \ No newline at end of file