Skip to content

Commit a437326

Browse files
committed
Merge pull request eugenp#343 from eugenp/pr/318
Spring Data Redis article examples
2 parents 95e2d06 + cc8d42c commit a437326

File tree

9 files changed

+310
-0
lines changed

9 files changed

+310
-0
lines changed

spring-data-redis/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Spring Data Redis
2+
3+
### Relevant Articles:
4+
- [Introduction to Spring Data Redis]
5+
6+
### Build the Project with Tests Running
7+
```
8+
mvn clean install
9+
```
10+
11+
### Run Tests Directly
12+
```
13+
mvn test
14+
```
15+

spring-data-redis/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.baeldung</groupId>
6+
<artifactId>sprint-data-redis</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<spring.version>4.2.2.RELEASE</spring.version>
13+
<spring-data-redis>1.6.2.RELEASE</spring-data-redis>
14+
<nosqlunit.version>0.8.0</nosqlunit.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.data</groupId>
20+
<artifactId>spring-data-redis</artifactId>
21+
<version>${spring-data-redis}</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>cglib</groupId>
26+
<artifactId>cglib-nodep</artifactId>
27+
<version>2.2</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>log4j</groupId>
32+
<artifactId>log4j</artifactId>
33+
<version>1.2.16</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>redis.clients</groupId>
38+
<artifactId>jedis</artifactId>
39+
<version>2.5.1</version>
40+
<type>jar</type>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework</groupId>
45+
<artifactId>spring-core</artifactId>
46+
<version>${spring.version}</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-context</artifactId>
52+
<version>${spring.version}</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.12</version>
59+
<scope>test</scope>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-test</artifactId>
65+
<version>${spring.version}</version>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>com.lordofthejars</groupId>
71+
<artifactId>nosqlunit-redis</artifactId>
72+
<version>${nosqlunit.version}</version>
73+
</dependency>
74+
75+
</dependencies>
76+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.spring.data.redis.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
7+
import org.springframework.data.redis.core.RedisTemplate;
8+
9+
@Configuration
10+
@ComponentScan("org.baeldung.spring.data.redis")
11+
public class RedisConfig {
12+
13+
@Bean
14+
JedisConnectionFactory jedisConnectionFactory() {
15+
return new JedisConnectionFactory();
16+
}
17+
18+
@Bean
19+
public RedisTemplate<String, Object> redisTemplate() {
20+
final RedisTemplate< String, Object> template = new RedisTemplate<String, Object>();
21+
template.setConnectionFactory(jedisConnectionFactory());
22+
return template;
23+
}
24+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.baeldung.spring.data.redis.model;
2+
3+
import java.io.Serializable;
4+
5+
public class Student implements Serializable {
6+
7+
public enum Gender {
8+
MALE, FEMALE
9+
}
10+
11+
private String id;
12+
private String name;
13+
private Gender gender;
14+
private int grade;
15+
16+
public Student(String id, String name, Gender gender, int grade) {
17+
this.id = id;
18+
this.name = name;
19+
this.gender = gender;
20+
this.grade = grade;
21+
}
22+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public Gender getGender() {
40+
return gender;
41+
}
42+
43+
public void setGender(Gender gender) {
44+
this.gender = gender;
45+
}
46+
47+
public int getGrade() {
48+
return grade;
49+
}
50+
51+
public void setGrade(int grade) {
52+
this.grade = grade;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", gender=" + gender + ", grade=" + grade + '}';
58+
}
59+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.spring.data.redis.repo;
2+
3+
import org.baeldung.spring.data.redis.model.Student;
4+
import org.springframework.stereotype.Component;
5+
6+
import java.util.Map;
7+
8+
public interface StudentRepository {
9+
10+
void saveStudent(Student person);
11+
12+
void updateStudent(Student student);
13+
14+
Student findStudent(String id);
15+
16+
Map<Object, Object> findAllStudents();
17+
18+
void deleteStudent(String id);
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.baeldung.spring.data.redis.repo;
2+
3+
import org.baeldung.spring.data.redis.model.Student;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.redis.core.RedisTemplate;
6+
import org.springframework.stereotype.Repository;
7+
8+
import java.util.Map;
9+
10+
@Repository
11+
public class StudentRepositoryImpl implements StudentRepository {
12+
13+
private static final String KEY = "Student";
14+
15+
@Autowired
16+
private RedisTemplate<String, Object> redisTemplate;
17+
18+
public void saveStudent(final Student student) {
19+
redisTemplate.opsForHash().put(KEY, student.getId(), student);
20+
}
21+
22+
public void updateStudent(final Student student) {
23+
redisTemplate.opsForHash().put(KEY, student.getId(), student);
24+
}
25+
26+
public Student findStudent(final String id) {
27+
return (Student) redisTemplate.opsForHash().get(KEY, id);
28+
}
29+
30+
public Map<Object, Object> findAllStudents() {
31+
return redisTemplate.opsForHash().entries(KEY);
32+
}
33+
34+
public void deleteStudent(final String id) {
35+
this.redisTemplate.opsForHash().delete(KEY, id);
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework" level="WARN" />
11+
<logger name="org.springframework.transaction" level="WARN" />
12+
13+
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
14+
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
15+
16+
<root level="INFO">
17+
<appender-ref ref="STDOUT" />
18+
</root>
19+
20+
</configuration>
855 Bytes
Loading
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.baeldung.spring.data.redis.repo;
2+
3+
import org.baeldung.spring.data.redis.config.RedisConfig;
4+
import org.baeldung.spring.data.redis.model.Student;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.test.context.ContextConfiguration;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
12+
import java.util.Map;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNull;
16+
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@ContextConfiguration(classes = RedisConfig.class)
19+
public class StudentRepositoryTest {
20+
21+
@Autowired
22+
private StudentRepository studentRepository;
23+
24+
@Test
25+
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
26+
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
27+
studentRepository.saveStudent(student);
28+
final Student retrievedStudent = studentRepository.findStudent(student.getId());
29+
assertEquals(student.getId(), retrievedStudent.getId());
30+
}
31+
32+
@Test
33+
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
34+
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
35+
studentRepository.saveStudent(student);
36+
student.setName("Richard Watson");
37+
studentRepository.saveStudent(student);
38+
final Student retrievedStudent = studentRepository.findStudent(student.getId());
39+
assertEquals(student.getName(), retrievedStudent.getName());
40+
}
41+
42+
@Test
43+
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
44+
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
45+
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
46+
studentRepository.saveStudent(engStudent);
47+
studentRepository.saveStudent(medStudent);
48+
final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents();
49+
assertEquals(retrievedStudent.size(), 2);
50+
}
51+
52+
@Test
53+
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
54+
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
55+
studentRepository.saveStudent(student);
56+
studentRepository.deleteStudent(student.getId());
57+
final Student retrievedStudent = studentRepository.findStudent(student.getId());
58+
assertNull(retrievedStudent);
59+
}
60+
}

0 commit comments

Comments
 (0)