From 8b75f3da06e5f88427864e1a0254c1c5f843e350 Mon Sep 17 00:00:00 2001 From: sameira Date: Fri, 25 Dec 2015 23:37:49 +0530 Subject: [PATCH] Adding Spring Data Redis Examples --- spring-data-radis/pom.xml | 78 ++++++++++++++++++ .../spring/data/radis/model/Student.java | 64 ++++++++++++++ .../data/radis/repo/StudentRepository.java | 17 ++++ .../radis/repo/StudentRepositoryImpl.java | 37 +++++++++ .../src/main/resources/logback.xml | 20 +++++ .../src/main/resources/spring-config.xml | 19 +++++ spring-data-radis/src/main/resources/test.png | Bin 0 -> 855 bytes .../radis/repo/StudentRepositoryTest.java | 58 +++++++++++++ 8 files changed, 293 insertions(+) create mode 100644 spring-data-radis/pom.xml create mode 100644 spring-data-radis/src/main/java/org/baeldung/spring/data/radis/model/Student.java create mode 100644 spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepository.java create mode 100644 spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepositoryImpl.java create mode 100644 spring-data-radis/src/main/resources/logback.xml create mode 100644 spring-data-radis/src/main/resources/spring-config.xml create mode 100644 spring-data-radis/src/main/resources/test.png create mode 100644 spring-data-radis/src/test/java/org/baeldung/spring/data/radis/repo/StudentRepositoryTest.java diff --git a/spring-data-radis/pom.xml b/spring-data-radis/pom.xml new file mode 100644 index 0000000000..4d3b467d7b --- /dev/null +++ b/spring-data-radis/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + + org.baeldung + sprint-data-radis + 0.0.1-SNAPSHOT + jar + + sprint-data-radis + + + 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-radis/src/main/java/org/baeldung/spring/data/radis/model/Student.java b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/model/Student.java new file mode 100644 index 0000000000..4b8a90943c --- /dev/null +++ b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/model/Student.java @@ -0,0 +1,64 @@ +package org.baeldung.spring.data.radis.model; + + +import java.io.Serializable; + +public class Student implements Serializable { + + private static final long serialVersionUID = -1907106213598514113L; + + 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-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepository.java b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepository.java new file mode 100644 index 0000000000..bc1201478c --- /dev/null +++ b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepository.java @@ -0,0 +1,17 @@ +package org.baeldung.spring.data.radis.repo; + +import org.baeldung.spring.data.radis.model.Student; + +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-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepositoryImpl.java b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepositoryImpl.java new file mode 100644 index 0000000000..3033020c24 --- /dev/null +++ b/spring-data-radis/src/main/java/org/baeldung/spring/data/radis/repo/StudentRepositoryImpl.java @@ -0,0 +1,37 @@ +package org.baeldung.spring.data.radis.repo; + +import org.baeldung.spring.data.radis.model.Student; +import org.springframework.data.redis.core.RedisTemplate; + +import java.util.Map; + +public class StudentRepositoryImpl implements StudentRepository { + + private static final String KEY = "Student"; + + private RedisTemplate redisTemplate; + + public void setRedisTemplate(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + public void saveStudent(final Student student) { + this.redisTemplate.opsForHash().put(KEY, student.getId(), student); + } + + public void updateStudent(final Student student) { + this.redisTemplate.opsForHash().put(KEY, student.getId(), student); + } + + public Student findStudent(final String id) { + return (Student)this.redisTemplate.opsForHash().get(KEY, id); + } + + public Map findAllStudents() { + return this.redisTemplate.opsForHash().entries(KEY); + } + + public void deleteStudent(final String id) { + this.redisTemplate.opsForHash().delete(KEY,id); + } +} diff --git a/spring-data-radis/src/main/resources/logback.xml b/spring-data-radis/src/main/resources/logback.xml new file mode 100644 index 0000000000..215eeede64 --- /dev/null +++ b/spring-data-radis/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-radis/src/main/resources/spring-config.xml b/spring-data-radis/src/main/resources/spring-config.xml new file mode 100644 index 0000000000..950cd9a6fd --- /dev/null +++ b/spring-data-radis/src/main/resources/spring-config.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-radis/src/main/resources/test.png b/spring-data-radis/src/main/resources/test.png new file mode 100644 index 0000000000000000000000000000000000000000..c3b5e8027635ff4f0a0b10c0a9a6aa7a014e6086 GIT binary patch literal 855 zcmex=6kE^cmKJ|O{KK0!WiZUHd? zL17V5QBfX#aS1UI2_X?t5k_Vf7B*H^PBu0!em)*P5fKr7L0%pn0bT(feo-D?UVdI4 zpg0dNAHM*Q$HT+J$HNcg@q#%Zh5rvQ2y!sUFvu`73NkPWGBOJ?{y)MX0rV0OKpYGv zIoQ})S%5M+0t`%y%uFn-%&bsZMkb)c1=$oC*g1rh3>^~-jRG5mMVuBM1ge%|WME=O zF^8Fjk%^U!0Vt;gv=3+$3p2tNMkYaKCBqLwfr*X6ijMzpG4L=04G?4&WUyx___R78 z`bEZ3n*%y$c1p?=OzsY{vOIcDRID^@mH5k5n_VmCLN*tlQtc~BqmaI3acZm^A1dHBrChNRU-KfIG`3a9F1Z+JCT zaBkFRt*1+W!zihZ}s%F&cxn?h~EIRD%9;ni-#KyOjOZ3l7 z_6Ks(XSKVYPbdvPm!tpIJj%Ot(S2`WC0AaXj1Ds{Yc~e@JYj`ND^=I^G)#%-Q1qX7 z;Q8}@p=SNS>z3K2PrXivv`$ 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