diff --git a/pom.xml b/pom.xml index f34c1936df..eb12eaf80d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.2.RELEASE + 2.1.0.RELEASE diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java new file mode 100644 index 0000000000..bd06e26e39 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java @@ -0,0 +1,40 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.repositories.BookRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +/** + * Created by João Nunes on 12/06/2023. + */ +@Component +public class DataInitializer implements CommandLineRunner { + + private final BookRepository bookRepository; + + public DataInitializer(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + + @Override + public void run(String... args) throws Exception { + Book bookDDD = new Book("Domain Driven Design", "123", "RandomHouse"); + + System.out.println("Id: " + bookDDD.getId()); + + Book savedDDD = bookRepository.save(bookDDD); + + System.out.println("Id: " + savedDDD.getId()); + + Book bookSIA = new Book("Spring in Action", "123", "RandomHouse"); + + Book savedSIA = bookRepository.save(bookSIA); + + bookRepository.findAll().forEach(book -> { + System.out.println("Book Id: " + book.getId()); + System.out.println("Book Title: " + book.getTitle()); + }); + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java new file mode 100644 index 0000000000..5ddf014346 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,73 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Objects; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + private String publisher; + + public Book() { + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + return Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + public Book(String title, String isbn, String publisher) { + this.title = title; + this.isbn = isbn; + this.publisher = publisher; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public String getPublisher() { + return publisher; + } + + public void setPublisher(String publisher) { + this.publisher = publisher; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java new file mode 100644 index 0000000000..66460e728b --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java @@ -0,0 +1,9 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Book; +import org.springframework.data.jpa.repository.JpaRepository; + + +// I could also have used the annotations +public interface BookRepository extends JpaRepository { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..b1d26ba35c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1,16 @@ +#spring.jpa.show-sql=true + +#Show SQL +spring.jpa.properties.hibernate.show_sql=true + +#Format SQL +spring.jpa.properties.hibernate.format_sql=true + +#Show bind values +logging.level.org.hibernate.type.descriptor.sql=trace + +spring.h2.console.enabled=true + + + + diff --git a/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java b/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java index e78ac1b703..2c7184d61c 100644 --- a/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java +++ b/src/test/java/guru/springframework/spring5webapp/Spring5webappApplicationTests.java @@ -1,16 +1,26 @@ package guru.springframework.spring5webapp; +import guru.springframework.spring5webapp.repositories.BookRepository; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + @RunWith(SpringRunner.class) @SpringBootTest public class Spring5webappApplicationTests { - @Test - public void contextLoads() { - } + @Autowired + BookRepository bookRepository; + + @Test + public void contextLoads() { + long count = bookRepository.count(); + assertThat(count).isGreaterThan(0); + + } }