First commit

This commit is contained in:
Michele Guarnaccia 2020-05-10 18:40:27 +02:00
parent b48f01d324
commit 4f7b0d6a64
7 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>spring-data-jpa-5</artifactId>
<name>spring-data-jpa-5</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<Customer, Long> {
@Cacheable("customers")
Customer findById(long id);
}

View File

@ -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;
}
}

View File

@ -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);
}