From ea9fb01dcda3a1719b4de6a55cdc83e3b655045e Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 15:18:05 +0300 Subject: [PATCH 01/12] 14. JPA Entities --- .../spring5webapp/domain/Author.java | 52 +++++++++++++++++++ .../spring5webapp/domain/Book.java | 51 ++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Author.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Book.java diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java new file mode 100644 index 0000000000..d26f003dac --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,52 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.Set; + +@Entity +public class Author { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String firstName; + private String lastName; + + @ManyToMany(mappedBy = "authors") + private Set books; + + public Author(String firstName, String lastName, Set books) { + this.firstName = firstName; + this.lastName = lastName; + this.books = books; + } + + public Author() { + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + +} 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..5600c84476 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,51 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.Set; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String title; + private String isbn; + + @ManyToMany + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name="book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors; + + public Book(String title, String isbn, Set authors) { + this.title = title; + this.isbn = isbn; + } + + public Book() { + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } +} From 7fad6fddd50e249605cb86e9a1dbd157237d21bc Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 17:07:27 +0300 Subject: [PATCH 02/12] 15. Equality in Hibernate --- .../spring5webapp/domain/Author.java | 23 +++++++++++++++++ .../spring5webapp/domain/Book.java | 25 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java index d26f003dac..9b837eabeb 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -48,5 +48,28 @@ public void setBooks(Set books) { this.books = books; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + + return id != null ? id.equals(author.id) : author.id == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java index 5600c84476..c1bffe8c29 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -48,4 +48,29 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + return id != null ? id.equals(book.id) : book.id == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } } From 589141a7fbf69e34610f2c857537200793e5a5e8 Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 17:12:45 +0300 Subject: [PATCH 03/12] 16. Spring Data Repositories --- .../spring5webapp/repositiries/AuthorRepository.java | 8 ++++++++ .../spring5webapp/repositiries/BookRepository.java | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/repositiries/AuthorRepository.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositiries/BookRepository.java diff --git a/src/main/java/guru/springframework/spring5webapp/repositiries/AuthorRepository.java b/src/main/java/guru/springframework/spring5webapp/repositiries/AuthorRepository.java new file mode 100644 index 0000000000..770198b386 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositiries/AuthorRepository.java @@ -0,0 +1,8 @@ +package guru.springframework.spring5webapp.repositiries; + +import guru.springframework.spring5webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository { + +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositiries/BookRepository.java b/src/main/java/guru/springframework/spring5webapp/repositiries/BookRepository.java new file mode 100644 index 0000000000..697b04e81a --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositiries/BookRepository.java @@ -0,0 +1,8 @@ +package guru.springframework.spring5webapp.repositiries; + +import guru.springframework.spring5webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { + +} From 1cf4fe27bce90c5b178ddbdfd0edae271b46bf5c Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 17:27:12 +0300 Subject: [PATCH 04/12] 17. Initializing Data with Spring --- .../bootstrap/BootStrapData.java | 45 +++++++++++++++++++ .../spring5webapp/domain/Author.java | 7 +-- .../spring5webapp/domain/Book.java | 8 ++-- 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java new file mode 100644 index 0000000000..dc84892cdd --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java @@ -0,0 +1,45 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.domain.Author; +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.repositiries.AuthorRepository; +import guru.springframework.spring5webapp.repositiries.BookRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class BootStrapData implements CommandLineRunner { + + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + + public BootStrapData(AuthorRepository authorRepository, BookRepository bookRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + } + + @Override + public void run(String... args) { + + Author eric = new Author("Eric", "Ewan"); + Book ddd = new Book("Qwerty", "asdewq"); + + eric.getBooks().add(ddd); + ddd.getAuthors().add(eric); + + authorRepository.save(eric); + bookRepository.save(ddd); + + Author rod = new Author("Rod", "Johnson"); + Book noEJB = new Book("J2EE", "sssssdddd"); + + rod.getBooks().add(noEJB); + noEJB.getAuthors().add(rod); + + authorRepository.save(rod); + bookRepository.save(noEJB); + + System.out.println("Started in Bootstrap"); + System.out.println("Number of Books: " + bookRepository.count()); + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java index 9b837eabeb..d757a47808 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -1,6 +1,7 @@ package guru.springframework.spring5webapp.domain; import javax.persistence.*; +import java.util.HashSet; import java.util.Set; @Entity @@ -13,12 +14,12 @@ public class Author { private String lastName; @ManyToMany(mappedBy = "authors") - private Set books; + private Set books = new HashSet<>(); - public Author(String firstName, String lastName, Set books) { + public Author(String firstName, String lastName/*, Set books*/) { this.firstName = firstName; this.lastName = lastName; - this.books = books; + //this.books = books; } public Author() { diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java index c1bffe8c29..c0ceae823f 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -1,6 +1,7 @@ package guru.springframework.spring5webapp.domain; import javax.persistence.*; +import java.util.HashSet; import java.util.Set; @Entity @@ -13,13 +14,14 @@ public class Book { private String isbn; @ManyToMany - @JoinTable(name = "author_book", joinColumns = @JoinColumn(name="book_id"), + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) - private Set authors; + private Set authors = new HashSet<>(); - public Book(String title, String isbn, Set authors) { + public Book(String title, String isbn/*, Set authors*/) { this.title = title; this.isbn = isbn; +// this.authors = authors; } public Book() { From c6b38f424bf6f0ea4da0044b2243a8706af73219 Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 17:49:42 +0300 Subject: [PATCH 05/12] Assignment 2: Add Publisher Entity --- .../bootstrap/BootStrapData.java | 17 +++- .../spring5webapp/domain/Publisher.java | 98 +++++++++++++++++++ .../repositiries/PublisherRepository.java | 7 ++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Publisher.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositiries/PublisherRepository.java diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java index dc84892cdd..adfd4e889f 100644 --- a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java @@ -2,8 +2,10 @@ import guru.springframework.spring5webapp.domain.Author; import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.domain.Publisher; import guru.springframework.spring5webapp.repositiries.AuthorRepository; import guru.springframework.spring5webapp.repositiries.BookRepository; +import guru.springframework.spring5webapp.repositiries.PublisherRepository; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @@ -12,10 +14,12 @@ public class BootStrapData implements CommandLineRunner { private final AuthorRepository authorRepository; private final BookRepository bookRepository; + private final PublisherRepository publisherRepository; - public BootStrapData(AuthorRepository authorRepository, BookRepository bookRepository) { + public BootStrapData(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { this.authorRepository = authorRepository; this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; } @Override @@ -41,5 +45,16 @@ public void run(String... args) { System.out.println("Started in Bootstrap"); System.out.println("Number of Books: " + bookRepository.count()); + + Publisher mm = new Publisher("M&M", "this street", "asd", "MN", "43000"); + Publisher nn = new Publisher("N&N", "that street", "asssd", "MN", "43003"); + Publisher ll = new Publisher("L&L", "the other street", "adddsd", "MN", "43020"); + + publisherRepository.save(mm); + publisherRepository.save(nn); + publisherRepository.save(ll); + + System.out.println("Number of Publishers: " + publisherRepository.count()); + } } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java new file mode 100644 index 0000000000..6bd9752636 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -0,0 +1,98 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Publisher { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String name; + private String addressLine1; + private String city; + private String state; + private String zip; + + public Publisher(String name, String addressLine1, String city, String state, String zip) { + this.name = name; + this.addressLine1 = addressLine1; + this.city = city; + this.state = state; + this.zip = zip; + } + + public Publisher() { + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddressLine1() { + return addressLine1; + } + + public void setAddressLine1(String addressLine1) { + this.addressLine1 = addressLine1; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Publisher publisher = (Publisher) o; + + return id != null ? id.equals(publisher.id) : publisher.id == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", name='" + name + '\'' + + ", addressLine1='" + addressLine1 + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositiries/PublisherRepository.java b/src/main/java/guru/springframework/spring5webapp/repositiries/PublisherRepository.java new file mode 100644 index 0000000000..ecf9e67ddf --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositiries/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repositiries; + +import guru.springframework.spring5webapp.domain.Publisher; +import org.springframework.data.repository.CrudRepository; + +public interface PublisherRepository extends CrudRepository { +} From 32aa48fa3c9bb988d279c21a3fb634fea70bf6d6 Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 18:37:47 +0300 Subject: [PATCH 06/12] 18. Publisher Relationships --- .../spring5webapp/bootstrap/BootStrapData.java | 18 +++++++++--------- .../spring5webapp/domain/Book.java | 12 ++++++++++++ .../spring5webapp/domain/Publisher.java | 18 ++++++++++++++---- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java index adfd4e889f..2a5e09a1cb 100644 --- a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java @@ -25,14 +25,22 @@ public BootStrapData(AuthorRepository authorRepository, BookRepository bookRepos @Override public void run(String... args) { + Publisher publisher = new Publisher("M&M", "this street", "asd", "MN", "43000"); + publisherRepository.save(publisher); + System.out.println("Number of Publishers: " + publisherRepository.count()); + Author eric = new Author("Eric", "Ewan"); Book ddd = new Book("Qwerty", "asdewq"); eric.getBooks().add(ddd); ddd.getAuthors().add(eric); + ddd.setPublisher(publisher); + publisher.getBooks().add(ddd); + authorRepository.save(eric); bookRepository.save(ddd); + publisherRepository.save(publisher); Author rod = new Author("Rod", "Johnson"); Book noEJB = new Book("J2EE", "sssssdddd"); @@ -45,16 +53,8 @@ public void run(String... args) { System.out.println("Started in Bootstrap"); System.out.println("Number of Books: " + bookRepository.count()); + System.out.println("Publishers Number of Books: " + publisher.getBooks().size()); - Publisher mm = new Publisher("M&M", "this street", "asd", "MN", "43000"); - Publisher nn = new Publisher("N&N", "that street", "asssd", "MN", "43003"); - Publisher ll = new Publisher("L&L", "the other street", "adddsd", "MN", "43020"); - - publisherRepository.save(mm); - publisherRepository.save(nn); - publisherRepository.save(ll); - - System.out.println("Number of Publishers: " + publisherRepository.count()); } } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java index c0ceae823f..d981a7779d 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -1,6 +1,7 @@ package guru.springframework.spring5webapp.domain; import javax.persistence.*; +import java.security.PublicKey; import java.util.HashSet; import java.util.Set; @@ -13,6 +14,9 @@ public class Book { private String title; private String isbn; + @ManyToOne + private Publisher publisher; + @ManyToMany @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) @@ -27,6 +31,14 @@ public Book(String title, String isbn/*, Set authors*/) { public Book() { } + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + public String getTitle() { return title; } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java index 6bd9752636..d746765cf5 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -1,9 +1,8 @@ package guru.springframework.spring5webapp.domain; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; @Entity public class Publisher { @@ -17,6 +16,10 @@ public class Publisher { private String state; private String zip; + @OneToMany + @JoinColumn(name = "publisher_id") + private Set books = new HashSet<>(); + public Publisher(String name, String addressLine1, String city, String state, String zip) { this.name = name; this.addressLine1 = addressLine1; @@ -28,6 +31,13 @@ public Publisher(String name, String addressLine1, String city, String state, St public Publisher() { } + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } public String getName() { return name; From 1e9485fa3b32222300826e3ab371246c885c5601 Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 19:05:20 +0300 Subject: [PATCH 07/12] fix --- .../springframework/spring5webapp/domain/Author.java | 10 +++++++++- .../springframework/spring5webapp/domain/Book.java | 8 ++++++++ .../spring5webapp/domain/Publisher.java | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java index d757a47808..337e01c0dc 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -11,8 +11,8 @@ public class Author { private Long id; private String firstName; - private String lastName; + private String lastName; @ManyToMany(mappedBy = "authors") private Set books = new HashSet<>(); @@ -25,6 +25,14 @@ public Author(String firstName, String lastName/*, Set books*/) { public Author() { } + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + public String getFirstName() { return firstName; } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java index d981a7779d..6ecc2218ec 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -31,6 +31,14 @@ public Book(String title, String isbn/*, Set authors*/) { public Book() { } + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + public Publisher getPublisher() { return publisher; } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java index d746765cf5..4c9620bbc4 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -31,6 +31,14 @@ public Publisher(String name, String addressLine1, String city, String state, St public Publisher() { } + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + public Set getBooks() { return books; } From 8c451267e9e2f967495486475442c234d2a610fa Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 21:08:13 +0300 Subject: [PATCH 08/12] fix2 --- pom.xml | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index f34c1936df..84d50b0a63 100644 --- a/pom.xml +++ b/pom.xml @@ -14,29 +14,19 @@ org.springframework.boot spring-boot-starter-parent - 2.1.2.RELEASE + 2.2.2.RELEASE - UTF-8 - UTF-8 - 1.8 + 11 - - org.springframework.boot - spring-boot-starter-actuator - org.springframework.boot spring-boot-starter-data-jpa - - org.springframework.boot - spring-boot-starter-thymeleaf - org.springframework.boot spring-boot-starter-web From f6e812f5ae8cc9716008b2a1c4bcbb258458fd7b Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Fri, 5 Jun 2020 21:08:36 +0300 Subject: [PATCH 09/12] 19. H2 Database Console --- src/main/resources/application.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..69b89983cb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.h2.console.enabled=true \ No newline at end of file From cd958dead2941fc22bfd1e0b69b4bd4216390608 Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Mon, 8 Jun 2020 13:53:36 +0300 Subject: [PATCH 10/12] 21. Configuring Spring MVC Controllers --- .../controllers/BookController.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/controllers/BookController.java diff --git a/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java new file mode 100644 index 0000000000..5998a8c201 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java @@ -0,0 +1,23 @@ +package guru.springframework.spring5webapp.controllers; + +import guru.springframework.spring5webapp.repositiries.BookRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class BookController { + + private final BookRepository bookRepository; + + public BookController(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + + @RequestMapping("/books") + public String getBooks(Model model) { + model.addAttribute("books", bookRepository.findAll()); + return "books"; + } +} From 689fc91046cdfbc462507d834daee38931a6ec75 Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Mon, 8 Jun 2020 18:46:24 +0300 Subject: [PATCH 11/12] 22. Thymeleaf Templates --- pom.xml | 167 +++++++++--------- .../bootstrap/BootStrapData.java | 1 + .../controllers/BookController.java | 2 +- .../spring5webapp/domain/Author.java | 1 - .../spring5webapp/domain/Book.java | 1 - src/main/resources/templates/books/list.html | 23 +++ 6 files changed, 110 insertions(+), 85 deletions(-) create mode 100644 src/main/resources/templates/books/list.html diff --git a/pom.xml b/pom.xml index 84d50b0a63..57bdc9db1b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,95 +1,98 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - guru.springframework - spring5webapp - 0.0.1-SNAPSHOT - jar + guru.springframework + spring5webapp + 0.0.1-SNAPSHOT + jar - spring5webapp - Demo project for Spring Boot + spring5webapp + Demo project for Spring Boot - - org.springframework.boot - spring-boot-starter-parent - 2.2.2.RELEASE - - + + org.springframework.boot + spring-boot-starter-parent + 2.2.2.RELEASE + + - - 11 - + + 11 + - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java index 2a5e09a1cb..16991890c9 100644 --- a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java @@ -47,6 +47,7 @@ public void run(String... args) { rod.getBooks().add(noEJB); noEJB.getAuthors().add(rod); + noEJB.setPublisher(publisher); authorRepository.save(rod); bookRepository.save(noEJB); diff --git a/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java index 5998a8c201..df2663c673 100644 --- a/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java +++ b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java @@ -18,6 +18,6 @@ public BookController(BookRepository bookRepository) { @RequestMapping("/books") public String getBooks(Model model) { model.addAttribute("books", bookRepository.findAll()); - return "books"; + return "books/list"; } } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java index 337e01c0dc..981a3ce400 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -78,7 +78,6 @@ public String toString() { "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + - ", books=" + books + '}'; } } diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java index 6ecc2218ec..5ba5e1d364 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -92,7 +92,6 @@ public String toString() { "id=" + id + ", title='" + title + '\'' + ", isbn='" + isbn + '\'' + - ", authors=" + authors + '}'; } } diff --git a/src/main/resources/templates/books/list.html b/src/main/resources/templates/books/list.html new file mode 100644 index 0000000000..10499dd655 --- /dev/null +++ b/src/main/resources/templates/books/list.html @@ -0,0 +1,23 @@ + + + + + Spring Framework Guru + + +

Book List

+ + + + + + + + + + + + +
IDTitlePublisher
123Spring in ActionWddx
+ + \ No newline at end of file From 5ebe33845e10b445290af514af91afead0e47593 Mon Sep 17 00:00:00 2001 From: vvoitsikhovskyi Date: Mon, 8 Jun 2020 19:00:30 +0300 Subject: [PATCH 12/12] Assignment 3 --- .../controllers/AuthorController.java | 22 ++++++++++++++++++ .../resources/templates/authors/list.html | 23 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java create mode 100644 src/main/resources/templates/authors/list.html diff --git a/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java b/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java new file mode 100644 index 0000000000..f1da4af028 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java @@ -0,0 +1,22 @@ +package guru.springframework.spring5webapp.controllers; + +import guru.springframework.spring5webapp.repositiries.AuthorRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class AuthorController { + + AuthorRepository authorRepository; + + public AuthorController(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @RequestMapping("/authors") + public String getAuthors(Model model) { + model.addAttribute("authors", authorRepository.findAll()); + return "authors/list"; + } +} diff --git a/src/main/resources/templates/authors/list.html b/src/main/resources/templates/authors/list.html new file mode 100644 index 0000000000..df6319abd3 --- /dev/null +++ b/src/main/resources/templates/authors/list.html @@ -0,0 +1,23 @@ + + + + + Spring Framework Guru + + +

Author List

+ + + + + + + + + + + + +
IDFirst NameLast Name
123First NameLast Name
+ + \ No newline at end of file