code samples for spring data key value

BAEL-1467
This commit is contained in:
Karan Khanna
2018-05-08 18:48:04 +02:00
parent daafd33aac
commit 635cd110b3
10 changed files with 445 additions and 0 deletions
@@ -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);
}
}
@@ -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);
}
}
@@ -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> {
}
@@ -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();
}
@@ -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);
}
}
@@ -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");
}
}
@@ -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 + '\'' +
'}';
}
}