We
have seen how to setup on your linux machine here
, now we will see how to perform basic CRUD operations using Spring
Data & Redis server
First of all we will be creating the Employee entity, plz note that we are using the Serializable interface which automatically mapps the objects against the key.
Employee.java
import java.io.Serializable; | |
public class Employee implements Serializable | |
{ | |
private static final long serialVersionUID = -8243145429438016231L; | |
public enum Gender{Male, Female} | |
private String id; | |
private String name; | |
private Gender gender; | |
private int age; | |
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 getAge() | |
{ | |
return age; | |
} | |
public void setAge(int age) | |
{ | |
this.age = age; | |
} | |
... | |
} |
EmployeeRepository.java
import java.util.Map; | |
import com.metamagicglobal.examples.redis.entity.Employees; | |
public interface EmployeeRepository | |
{ | |
public void addEmployee(Employees employee); | |
public Employees getEmployeeById(String id); | |
public Map<Object, Object> getAllEmployees(); | |
void updateEmployee(Employees employee); | |
public void delEmployeeById(String id); | |
} |
public class EmployeeRepositoryImpl implements EmployeeRepository | |
{ | |
private RedisTemplate<String, Employees> redisTemplate; | |
private static String EMP_KEY = "Employees"; | |
public RedisTemplate<String, Employees> getRedisTemplate() | |
{ | |
return redisTemplate; | |
} | |
public void setRedisTemplate(RedisTemplate<String, Employees> redisTemplate) | |
{ | |
this.redisTemplate = redisTemplate; | |
} | |
@Override | |
public void addEmployee(Employees emp) | |
{ | |
this.redisTemplate.opsForHash().put(EMP_KEY, emp.getId(), emp); | |
} | |
@Override | |
public Employees getEmployeeById(String id) | |
{ | |
return (Employees)this.redisTemplate.opsForHash().get(EMP_KEY, id); | |
} | |
@Override | |
public Map<Object,Object> getAllEmployees() | |
{ | |
return this.redisTemplate.opsForHash().entries(EMP_KEY); | |
} | |
@Override | |
public void delEmployeeById(String id) | |
{ | |
this.redisTemplate.opsForHash().delete(EMP_KEY,id); | |
} | |
@Override | |
public void updateEmployee(Employees emp) { | |
// TODO Auto-generated method stub | |
this.redisTemplate.opsForHash().put(EMP_KEY, emp.getId(), emp); | |
} | |
} |
EmployeeTest.java
public class EmployeeTest { | |
public static void main(String[] args) { | |
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( | |
new ClassPathResource("resources/spring-config.xml").getPath()); | |
EmployeeRepository empRepo = (EmployeeRepository) context.getBean("empRepo"); | |
Employees emp = new Employees(); | |
emp.setId("101"); | |
emp.setAge(55); | |
emp.setGender(Gender.Female); | |
emp.setName("Jenny Joe"); | |
empRepo.addEmployee(emp); | |
... | |
Map<Object, Object> empMap = empRepo.getAllEmployees(); | |
System.out.println("Display All Employees"); | |
System.out.println(empMap); | |
System.out.println("Find Employee by ID : " + empRepo.getEmployeeById("103")); | |
emp3.setAge(40); | |
empRepo.updateEmployee(emp3); | |
System.out.println("Employee after update : " + empRepo.getEmployeeById("103")); | |
System.out.println("Deleting Employee ID : 102 "); | |
empRepo.delEmployeeById("102"); | |
System.out.println("Display All Employees"); | |
empMap = empRepo.getAllEmployees(); | |
System.out.println(empMap); | |
context.close(); | |
} | |
} |
Output
Display All Employees
{103=
Employee - ID=103, Name=Ken Peg, Gender=Male, Age=25, 102=
Employee - ID=102, Name=John Doe, Gender=Male, Age=60, 101=
Employee - ID=101, Name=Jenny Joe, Gender=Female, Age=55}
Find Employee by ID :
Employee - ID=103, Name=Ken Peg, Gender=Male, Age=25
Employee after update :
Employee - ID=103, Name=Ken Peg, Gender=Male, Age=40
Deleting Employee ID : 2
Display All Employees
{103=
Employee - ID=103, Name=Ken Peg, Gender=Male, Age=40, 101=
Employee - ID=101, Name=Jenny Joe, Gender=Female, Age=55}
Comments
Post a Comment