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
@@ -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 +
'}';
}
}
@@ -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);
}
@@ -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);
}
}
@@ -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>
@@ -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