Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<module>spring-data-couchbase-2</module>
<module>persistence-modules/spring-data-dynamodb</module>
<module>spring-data-elasticsearch</module>
<module>spring-data-keyvalue</module>
<module>spring-data-mongodb</module>
<module>persistence-modules/spring-data-neo4j</module>
<module>persistence-modules/spring-data-redis</module>
Expand Down
37 changes: 37 additions & 0 deletions spring-data-keyvalue/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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>com.baeldung</groupId>
<artifactId>spring-data-keyvalue</artifactId>
<version>1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath />
</parent>

<properties>
<spring-data-keyvalue.version>2.0.3.RELEASE</spring-data-keyvalue.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.spring.data.keyvalue;

import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.keyvalue.core.KeyValueAdapter;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.map.MapKeyValueAdapter;

@Configuration
public class Configurations {

//To be used only if @EnableMapRepositories is not used.
//Else @EnableMapRepositories gives us a template as well.
@Bean("keyValueTemplate")
public KeyValueOperations keyValueTemplate() {
return new KeyValueTemplate(keyValueAdapter());

}

@Bean
public KeyValueAdapter keyValueAdapter() {
return new MapKeyValueAdapter(ConcurrentHashMap.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.spring.data.keyvalue;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.map.repository.config.EnableMapRepositories;

@SpringBootApplication
@EnableMapRepositories
public class SpringDataKeyValueApplication {

public static void main(String[] args) {
SpringApplication.run(SpringDataKeyValueApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.spring.data.keyvalue.repositories;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.baeldung.spring.data.keyvalue.vo.Employee;

@Repository("employeeRepository")
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.spring.data.keyvalue.services;

import com.baeldung.spring.data.keyvalue.vo.Employee;

public interface EmployeeService {

void save(Employee employee);

Employee get(Integer id);

Iterable<Employee> fetchAll();

void update(Employee employee);

void delete(Integer id);

Iterable<Employee> getSortedListOfEmployeesBySalary();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baeldung.spring.data.keyvalue.services.impl;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.domain.Sort;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
import org.springframework.stereotype.Service;

import com.baeldung.spring.data.keyvalue.services.EmployeeService;
import com.baeldung.spring.data.keyvalue.vo.Employee;

@Service("employeeServicesWithKeyValueTemplate")
@DependsOn("keyValueTemplate")
public class EmployeeServicesWithKeyValueTemplate implements EmployeeService {

@Autowired
@Qualifier("keyValueTemplate")
KeyValueTemplate keyValueTemplate;

@Override
public void save(Employee employee) {
keyValueTemplate.insert(employee);
}

@Override
public Employee get(Integer id) {
Optional<Employee> employee = keyValueTemplate.findById(id, Employee.class);
return employee.isPresent() ? employee.get() : null;
}

@Override
public Iterable<Employee> fetchAll() {
return keyValueTemplate.findAll(Employee.class);
}

@Override
public void update(Employee employee) {
keyValueTemplate.update(employee);
}

@Override
public void delete(Integer id) {
keyValueTemplate.delete(id, Employee.class);
}

@Override
public Iterable<Employee> getSortedListOfEmployeesBySalary() {
KeyValueQuery query = new KeyValueQuery();
query.setSort(new Sort(Sort.Direction.DESC, "salary"));
return keyValueTemplate.find(query, Employee.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.spring.data.keyvalue.services.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baeldung.spring.data.keyvalue.repositories.EmployeeRepository;
import com.baeldung.spring.data.keyvalue.services.EmployeeService;
import com.baeldung.spring.data.keyvalue.vo.Employee;

@Service("employeeServicesWithRepository")
public class EmployeeServicesWithRepository implements EmployeeService {

@Autowired
EmployeeRepository employeeRepository;


@Override
public void save(Employee employee) {
employeeRepository.save(employee);
}

@Override
public Iterable<Employee> fetchAll() {
return employeeRepository.findAll();

}

@Override
public Employee get(Integer id) {
return employeeRepository.findById(id).get();
}

@Override
public void update(Employee employee) {
employeeRepository.save(employee);

}

@Override
public void delete(Integer id) {
employeeRepository.deleteById(id);
}

public Iterable<Employee> getSortedListOfEmployeesBySalary() {
throw new RuntimeException("Method not supported by CRUDRepository");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.baeldung.spring.data.keyvalue.vo;

import java.io.Serializable;

import org.springframework.data.annotation.Id;
import org.springframework.data.keyvalue.annotation.KeySpace;

@KeySpace("employees")
public class Employee implements Serializable {

@Id
private Integer id;

private String name;

private String department;

private String salary;

public Employee(Integer id, String name, String department, String salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public String getSalary() {
return salary;
}

public void setSalary(String salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", department='" + department + '\'' +
", salary='" + salary + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.baeldung.spring.data.keyvalue.services.test;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.spring.data.keyvalue.SpringDataKeyValueApplication;
import com.baeldung.spring.data.keyvalue.services.EmployeeService;
import com.baeldung.spring.data.keyvalue.vo.Employee;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringDataKeyValueApplication.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EmployeeServicesWithKeyValueTemplateTest {

@Autowired
@Qualifier("employeeServicesWithKeyValueTemplate")
EmployeeService employeeService;

@Autowired
@Qualifier("keyValueTemplate")
KeyValueTemplate keyValueTemplate;

static Employee employee1;

static Employee employee2;

@BeforeClass
public static void setUp() {
employee1 = new Employee(1, "Karan", "IT", "5000");
employee2 = new Employee(2, "Jack", "HR", "2000");
}

@Test
public void test1_whenEmployeeSaved_thenEmployeeIsAddedToMap() {
employeeService.save(employee1);
assertEquals(keyValueTemplate.findById(1, Employee.class).get(), employee1);
}

@Test
public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() {
Employee employeeFetched = employeeService.get(1);
assertEquals(employeeFetched, employee1);
}

@Test
public void test3_whenEmployeesFetched_thenEmployeesAreReturnedFromMap() {
List<Employee> employees = (List<Employee>)employeeService.fetchAll();
assertEquals(employees.size(), 1);
assertEquals(employees.get(0), employee1);
}

@Test
public void test4_whenEmployeeUpdated_thenEmployeeIsUpdatedToMap() {
employee1.setName("Pawan");
employeeService.update(employee1);
assertEquals(keyValueTemplate.findById(1, Employee.class).get().getName(),"Pawan");
}

@Test
public void test5_whenSortedEmployeesFetched_thenEmployeesAreReturnedFromMapInOrder() {
employeeService.save(employee2);
List<Employee> employees = (List<Employee>)employeeService.getSortedListOfEmployeesBySalary();
assertEquals(employees.size(), 2);
assertEquals(employees.get(0), employee1);
assertEquals(employees.get(1), employee2);
}

@Test
public void test6_whenEmployeeDeleted_thenEmployeeIsRemovedMap() {
employeeService.delete(1);
assertEquals(keyValueTemplate.findById(1, Employee.class).isPresent(), false);
}



}
Loading