diff --git a/pom.xml b/pom.xml index f34c1936df..0cdf23421b 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,15 @@ org.springframework.boot spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot spring-boot-starter-thymeleaf 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..d3b2ef4ea4 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java @@ -0,0 +1,68 @@ +package guru.springframework.spring5webapp.bootstrap; + +import ch.qos.logback.core.net.SyslogOutputStream; +import guru.springframework.spring5webapp.domain.Author; +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.domain.Publisher; +import guru.springframework.spring5webapp.repository.AuthorRepository; +import guru.springframework.spring5webapp.repository.BookRepository; +import guru.springframework.spring5webapp.repository.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) throws Exception { + + System.out.println("Started in Bootstrap"); + + Publisher publisher = new Publisher(); + publisher.setName("SFG Publishing"); + publisher.setCity("St Petersburg"); + publisher.setState("FL"); + + publisherRepository.save(publisher); + + System.out.println("Publisher Count: " + publisherRepository.count()); + + Author eric = new Author("Eric", "Evans"); + Book ddd = new Book("Domain Driven Design", "123123"); + 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 Development without EJB", "3939459459"); + rod.getBooks().add(noEJB); + noEJB.getAuthors().add(rod); + + noEJB.setPublisher(publisher); + publisher.getBooks().add(noEJB); + + authorRepository.save(rod); + bookRepository.save(noEJB); + publisherRepository.save(publisher); + + System.out.println("Number of Books: " + bookRepository.count()); + System.out.println("Publisher Number of Books: " + publisher.getBooks().size()); + } +} \ No newline at end of file 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..e9161cbde1 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java @@ -0,0 +1,24 @@ +package guru.springframework.spring5webapp.controllers; + + +import guru.springframework.spring5webapp.repository.AuthorRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class AuthorController { + + private AuthorRepository authorRepository; + + public AuthorController(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @RequestMapping("templates/authors") + public String getAuthors(Model model) + { + model.addAttribute("authors",authorRepository.findAll()); //this "authors" key will be referenced in View to access the value of authors + 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..1dfc1600e4 --- /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.repository.BookRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class BookController { + + private BookRepository bookRepository; + + public BookController(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @RequestMapping("templates/books") + public String getBooks(Model model){ //model will return the result to the View and in view we will decide how to return the data + model.addAttribute("books",bookRepository.findAll()); //model will have an attribute books and it will contain the list of 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 new file mode 100644 index 0000000000..64d0fcaf3f --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,84 @@ +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() { + } + + public Author(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + 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 String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + // ", 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; + } +} \ No newline at end of file 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..aad3e40e90 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,98 @@ +package guru.springframework.spring5webapp.domain; + + +import javax.persistence.*; +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() { + } + + public Book(String title, String isbn) { + this.title = title; + this.isbn = isbn; + } + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + 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 Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + // ", authors=" + authors + + '}'; + } + + @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; + } +} \ No newline at end of file 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..5bb6ec5811 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -0,0 +1,110 @@ +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() { + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", name='" + name + '\'' + + ", addressLine1='" + addressLine1 + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", 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; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + 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; + } +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/spring5webapp/repository/AuthorRepository.java b/src/main/java/guru/springframework/spring5webapp/repository/AuthorRepository.java new file mode 100644 index 0000000000..61ed1cce21 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repository/AuthorRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repository; + +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/repository/BookRepository.java b/src/main/java/guru/springframework/spring5webapp/repository/BookRepository.java new file mode 100644 index 0000000000..005c7486cd --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repository/BookRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repository; + +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/repository/PublisherRepository.java b/src/main/java/guru/springframework/spring5webapp/repository/PublisherRepository.java new file mode 100644 index 0000000000..014858aef7 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repository/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repository; + +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..1dce12675c --- /dev/null +++ b/src/main/resources/templates/authors/list.html @@ -0,0 +1,31 @@ + + + + + Title + + +

Authors List

+ + + + + + + + + + + + + + + + + +
IdFirst 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..05331210d9 --- /dev/null +++ b/src/main/resources/templates/books/list.html @@ -0,0 +1,25 @@ + + + + + Title + + +

Book List

+ + + + + + + + + + + + +
IDTitlePublisher
123 Spring in ActionWrox
+ + + + \ No newline at end of file