[BAEL-9538] - Move persistence-related modules into the persistence folder
This commit is contained in:
+28
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -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> {
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.spring.data.keyvalue.services;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.baeldung.spring.data.keyvalue.vo.Employee;
|
||||
|
||||
public interface EmployeeService {
|
||||
|
||||
void save(Employee employee);
|
||||
|
||||
Optional<Employee> get(Integer id);
|
||||
|
||||
Iterable<Employee> fetchAll();
|
||||
|
||||
void update(Employee employee);
|
||||
|
||||
void delete(Integer id);
|
||||
|
||||
Iterable<Employee> getSortedListOfEmployeesBySalary();
|
||||
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
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 Optional<Employee> get(Integer id) {
|
||||
return keyValueTemplate.findById(id, Employee.class);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.spring.data.keyvalue.services.impl;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
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 Optional<Employee> get(Integer id) {
|
||||
return employeeRepository.findById(id);
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -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 {
|
||||
|
||||
@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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user