Adding Spring Data Redis Examples

This commit is contained in:
sameira 2015-12-25 23:54:54 +05:30
parent 8b75f3da06
commit 31b5d9ff16
8 changed files with 293 additions and 0 deletions

78
spring-data-redis/pom.xml Normal file
View File

@ -0,0 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>sprint-data-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sprint-data-redis</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.2.RELEASE</spring.version>
<spring-data-redis>1.6.2.RELEASE</spring-data-redis>
<nosqlunit.version>0.8.0</nosqlunit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.lordofthejars</groupId>
<artifactId>nosqlunit-redis</artifactId>
<version>${nosqlunit.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,64 @@
package org.baeldung.spring.data.redis.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 +
'}';
}
}

View File

@ -0,0 +1,17 @@
package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.model.Student;
import java.util.Map;
public interface StudentRepository {
void saveStudent(Student person);
void updateStudent(Student student);
Student findStudent(String id);
Map<Object, Object> findAllStudents();
void deleteStudent(String id);
}

View File

@ -0,0 +1,37 @@
package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.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<String, Student> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Student> 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<Object, Object> findAllStudents() {
return this.redisTemplate.opsForHash().entries(KEY);
}
public void deleteStudent(final String id) {
this.redisTemplate.opsForHash().delete(KEY,id);
}
}

View File

@ -0,0 +1,20 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:use-pool="true" />
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"/>
<bean id="studentRepository" class="org.baeldung.spring.data.redis.repo.StudentRepositoryImpl">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
</beans>

Binary file not shown.

After

Width:  |  Height:  |  Size: 855 B

View File

@ -0,0 +1,58 @@
package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.model.Student;
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(locations = {"classpath:/spring-config.xml"})
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<Object, Object> 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);
}
}