diff --git a/pom.xml b/pom.xml index 4495165e07..eb12eaf80d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M5 + 2.1.0.RELEASE diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java new file mode 100644 index 0000000000..53d074c71c --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java @@ -0,0 +1,60 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.model.Author; +import guru.springframework.spring5webapp.model.Book; +import guru.springframework.spring5webapp.model.Publisher; +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import guru.springframework.spring5webapp.repositories.BookRepository; +import guru.springframework.spring5webapp.repositories.PublisherRepository; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; + +/** + * Created by jt on 5/16/17. + */ +@Component +public class DevBootstrap implements ApplicationListener { + + private AuthorRepository authorRepository; + private BookRepository bookRepository; + private PublisherRepository publisherRepository; + + public DevBootstrap(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { + initData(); + } + + private void initData(){ + + Publisher publisher = new Publisher(); + publisher.setName("foo"); + publisher.setAddress("12th Street, LA"); + publisherRepository.save(publisher); + + //Eric + Author eric = new Author("Eric", "Evans"); + Book ddd = new Book("Domain Driven Design", "1234", publisher); + eric.getBooks().add(ddd); + ddd.getAuthors().add(eric); + + authorRepository.save(eric); + bookRepository.save(ddd); + + + //Rod + Author rod = new Author("Rod", "Johnson"); + Book noEJB = new Book("J2EE Development without EJB", "23444", publisher ); + rod.getBooks().add(noEJB); + noEJB.getAuthors().add(rod); + + authorRepository.save(rod); + bookRepository.save(noEJB); + } +} 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..374aeb8ec2 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java @@ -0,0 +1,27 @@ +package guru.springframework.spring5webapp.controllers; + +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by jt on 5/18/17. + */ +@Controller +public class AuthorController { + + private AuthorRepository authorRepository; + + public AuthorController(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @RequestMapping("/authors") + public String getAuthors(Model model){ + + model.addAttribute("authors", authorRepository.findAll()); + + return "authors"; + } +} 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..84399cb87d --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java @@ -0,0 +1,27 @@ +package guru.springframework.spring5webapp.controllers; + +import guru.springframework.spring5webapp.repositories.BookRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by jt on 5/18/17. + */ +@Controller +public class BookController { + + private BookRepository bookRepository; + + public BookController(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @RequestMapping("/books") + public String getBooks(Model model){ + + model.addAttribute("books", bookRepository.findAll()); + + return "books"; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/model/Author.java b/src/main/java/guru/springframework/spring5webapp/model/Author.java new file mode 100644 index 0000000000..1768e9c794 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Author.java @@ -0,0 +1,92 @@ +package guru.springframework.spring5webapp.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +/** + * Created by jt on 5/16/17. + */ +@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 Author(String firstName, String lastName, Set books) { + this.firstName = firstName; + this.lastName = lastName; + this.books = books; + } + + 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 + '\'' + + ", books=" + books + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/model/Book.java b/src/main/java/guru/springframework/spring5webapp/model/Book.java new file mode 100644 index 0000000000..1ccc0953df --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Book.java @@ -0,0 +1,107 @@ +package guru.springframework.spring5webapp.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +/** + * Created by jt on 5/16/17. + */ +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + + @OneToOne + 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, Publisher publisher) { + this.title = title; + this.isbn = isbn; + this.publisher = publisher; + } + + public Book(String title, String isbn, Publisher publisher, Set authors) { + this.title = title; + this.isbn = isbn; + this.publisher = publisher; + this.authors = authors; + } + + 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 Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.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; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", publisher='" + publisher + '\'' + + ", authors=" + authors + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/model/Publisher.java b/src/main/java/guru/springframework/spring5webapp/model/Publisher.java new file mode 100644 index 0000000000..a7e22888a2 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Publisher.java @@ -0,0 +1,67 @@ +package guru.springframework.spring5webapp.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * Created by jt on 5/17/17. + */ +@Entity +public class Publisher { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String name; + private String address; + + 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 getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @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 + '\'' + + ", address='" + address + '\'' + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java new file mode 100644 index 0000000000..e0946f3e5c --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java @@ -0,0 +1,10 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.model.Author; +import org.springframework.data.repository.CrudRepository; + +/** + * Created by jt on 5/16/17. + */ +public interface AuthorRepository extends CrudRepository { +} 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..a38ff57e5f --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java @@ -0,0 +1,10 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.model.Book; +import org.springframework.data.repository.CrudRepository; + +/** + * Created by jt on 5/16/17. + */ +public interface BookRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java new file mode 100644 index 0000000000..63a986bcf6 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java @@ -0,0 +1,10 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.model.Publisher; +import org.springframework.data.repository.CrudRepository; + +/** + * Created by jt on 5/17/17. + */ +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.html b/src/main/resources/templates/authors.html new file mode 100644 index 0000000000..48e1668d68 --- /dev/null +++ b/src/main/resources/templates/authors.html @@ -0,0 +1,24 @@ + + + + + Spring Framework Guru + + +

Author List

+ + + + + + + + + + + + +
IDFirst NameLast
123JoeBuck
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/books.html b/src/main/resources/templates/books.html new file mode 100644 index 0000000000..6796e39e28 --- /dev/null +++ b/src/main/resources/templates/books.html @@ -0,0 +1,24 @@ + + + + + Spring Framework Guru + + +

Book List

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