diff --git a/pom.xml b/pom.xml index f34c1936df..57bdc9db1b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,105 +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.1.2.RELEASE - - + + org.springframework.boot + spring-boot-starter-parent + 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 - + + + 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 new file mode 100644 index 0000000000..16991890c9 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java @@ -0,0 +1,61 @@ +package guru.springframework.spring5webapp.bootstrap; + +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; + +@Component +public class BootStrapData implements CommandLineRunner { + + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + private final PublisherRepository publisherRepository; + + public BootStrapData(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @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"); + + rod.getBooks().add(noEJB); + noEJB.getAuthors().add(rod); + noEJB.setPublisher(publisher); + + authorRepository.save(rod); + bookRepository.save(noEJB); + + System.out.println("Started in Bootstrap"); + System.out.println("Number of Books: " + bookRepository.count()); + System.out.println("Publishers Number of Books: " + publisher.getBooks().size()); + + + } +} 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/java/guru/springframework/spring5webapp/controllers/BookController.java b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java new file mode 100644 index 0000000000..df2663c673 --- /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/list"; + } +} 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..981a3ce400 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,83 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.HashSet; +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 = new HashSet<>(); + + public Author(String firstName, String lastName/*, Set books*/) { + this.firstName = firstName; + this.lastName = lastName; + //this.books = books; + } + + public Author() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + 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; + } + + @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 + '\'' + + '}'; + } +} 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..5ba5e1d364 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,97 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.security.PublicKey; +import java.util.HashSet; +import java.util.Set; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + 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")) + private Set authors = new HashSet<>(); + + public Book(String title, String isbn/*, Set authors*/) { + this.title = title; + this.isbn = isbn; +// this.authors = authors; + } + + public Book() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + 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; + } + + @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 + '\'' + + '}'; + } +} 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..4c9620bbc4 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -0,0 +1,116 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +@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; + + @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; + this.city = city; + this.state = state; + this.zip = zip; + } + + public Publisher() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + 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/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 { + +} 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 { +} 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 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 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