From 4f7b0d6a645625d115293133f48eb993a6d7b733 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 10 May 2020 18:40:27 +0200 Subject: [PATCH] First commit --- persistence-modules/spring-data-jpa-5/pom.xml | 58 +++++++++++++++++++ .../main/java/com/baeldung/Application.java | 58 +++++++++++++++++++ .../java/com/baeldung/model/Customer.java | 27 +++++++++ .../java/com/baeldung/model/CustomerDto.java | 34 +++++++++++ .../repository/CustomerRepository.java | 13 +++++ .../com/baeldung/service/CustomerService.java | 50 ++++++++++++++++ .../com/baeldung/util/CustomerMapper.java | 15 +++++ 7 files changed, 255 insertions(+) create mode 100644 persistence-modules/spring-data-jpa-5/pom.xml create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java diff --git a/persistence-modules/spring-data-jpa-5/pom.xml b/persistence-modules/spring-data-jpa-5/pom.xml new file mode 100644 index 0000000000..df1cc6c0c4 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + spring-data-jpa-5 + spring-data-jpa-5 + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.mapstruct + mapstruct-jdk8 + 1.3.1.Final + provided + + + org.springframework.boot + spring-boot-starter-cache + + + + + src/main/java + + + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + org.mapstruct + mapstruct-processor + 1.3.1.Final + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..64d0a5e6c6 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java @@ -0,0 +1,58 @@ +package com.baeldung; + +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; +import com.baeldung.service.CustomerService; + +@SpringBootApplication @EnableCaching +public class Application { + + @Autowired CustomerService service; + @Autowired CacheManager cacheManager; + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) throws Exception { + logger.info("-- BASIC LOAD AND SAVE --"); + basicLoadAndSave(); + logger.info("-- BASIC LOAD AND SAVE + MAPPER --"); + basicLoadAndSaveWithMapper(); + return null; + } + + private void basicLoadAndSave() { + Customer myCustomer = service.addCustomer("John", "Doe"); + logger.info("Insert -- " + myCustomer.toString()); + myCustomer = service.updateCustomer(myCustomer.id, "john@doe.com", "+00", "Route 66"); + logger.info("Update -- " + myCustomer.toString()); + } + + private void basicLoadAndSaveWithMapper() { + CustomerDto dto = new CustomerDto(); + dto.firstName = "Johnny"; + dto.lastName = "Doe"; + Customer entity = service.addCustomer(dto); + logger.info("Insert -- " + entity.toString()); + CustomerDto dto2 = new CustomerDto(); + dto2.id = entity.id; + dto2.address = "Mountain View"; + dto2.email = "doe@mail.com"; + dto2.phone = "+44"; + entity = service.updateCustomer(dto2); + logger.info("Update -- " + entity.toString()); + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java new file mode 100644 index 0000000000..91808e7971 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java @@ -0,0 +1,27 @@ +package com.baeldung.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Customer { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + public String firstName; + public String lastName; + public String address; + public String email; + public String phone; + //... + public String phone99; + + public Customer() {} + + @Override public String toString() { + return String.format("Customer %s %s, Address: %s, Email: %s, Phone: %s", + this.firstName, this.lastName, this.address, this.email, this.phone); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java new file mode 100644 index 0000000000..8f7803fc32 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -0,0 +1,34 @@ +package com.baeldung.model; + +public class CustomerDto { + public long id; + public String firstName; + public String lastName; + public String address; + public String email; + public String phone; + //... + public String phone99; + + public CustomerDto() {} + + public CustomerDto(Customer c) { + this.id = c.id; + this.firstName = c.firstName; + this.lastName = c.lastName; + this.address = c.address; + this.email = c.email; + this.phone = c.phone; + } + + public Customer convertToEntity() { + Customer c = new Customer(); + c.id = id; + c.firstName = firstName; + c.lastName = lastName; + c.address = address; + c.email = email; + c.phone = phone; + return c; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java new file mode 100644 index 0000000000..dcde6c3b46 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.repository; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.model.Customer; + +@Repository +public interface CustomerRepository extends CrudRepository { + @Cacheable("customers") + Customer findById(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java new file mode 100644 index 0000000000..56afc9e80d --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -0,0 +1,50 @@ +package com.baeldung.service; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; +import com.baeldung.repository.CustomerRepository; +import com.baeldung.util.CustomerMapper; + +@Service @Transactional +public class CustomerService { + + @Autowired CustomerRepository repo; + @Autowired CustomerMapper mapper; + + public Customer addCustomer(String firstName, String lastName) { + Customer myCustomer = new Customer(); + myCustomer.firstName = firstName; + myCustomer.lastName = lastName; + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(long id, String email, String phone, String address) { + Customer myCustomer = repo.findById(id); + myCustomer.address = address; + myCustomer.email = email; + myCustomer.phone = phone; + repo.save(myCustomer); + return myCustomer; + } + + public Customer addCustomer(CustomerDto dto) { + Customer myCustomer = new Customer(); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(CustomerDto dto) { + Customer myCustomer = repo.findById(dto.id); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java new file mode 100644 index 0000000000..d4f264e33a --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java @@ -0,0 +1,15 @@ +package com.baeldung.util; + +import org.mapstruct.BeanMapping; +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; +import org.mapstruct.NullValuePropertyMappingStrategy; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; + +@Mapper(componentModel = "spring") +public interface CustomerMapper { + @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) + void updateCustomerFromDto(CustomerDto dto, @MappingTarget Customer entity); +}