Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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.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");
Author eric = new Author("Eric", "Evans");
Book ddd = new Book("Design Book", "12342123");
Author rod = new Author("Rod", "Johnson");
Book noEJB = new Book("J2EE Development without EJB", "221421321");
publisherRepository.save(publisher);

eric.getBooks().add(ddd);
ddd.getAuthors().add(eric);
ddd.setPublisher(publisher);
publisher.getBooks().add(ddd);

authorRepository.save(eric);
bookRepository.save(ddd);
publisherRepository.save(publisher);

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("Started in Bootstrap");
System.out.println("Number of books: " + bookRepository.count());
System.out.println("Number of authors: " + authorRepository.count());
System.out.println("Publisher Number of Books: " + publisher.getBooks().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package guru.springframework.spring5webapp.controller;

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 final AuthorRepository authorRepository;

public AuthorController(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}

@RequestMapping("/authors")
public String getAuthors(Model model){
model.addAttribute("authors", authorRepository.findAll());
return "authors/list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package guru.springframework.spring5webapp.controller;

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 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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package guru.springframework.spring5webapp.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;
private String lastName;

@ManyToMany(mappedBy = "authors")
private Set<Book> books = new HashSet<>();

public Author(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}

@Override
public String toString() {
return "Author{" +
"id=" + id +
", name='" + name + '\'' +
", 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 Objects.equals(id, author.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
60 changes: 60 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/domain/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package guru.springframework.spring5webapp.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
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<Author> authors = new HashSet<>();

public Book(String title, String isbn) {
this.title = title;
this.isbn = isbn;
}

@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 Objects.equals(id, book.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package guru.springframework.spring5webapp.domain;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Data
@NoArgsConstructor
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<Book> books = new HashSet<>();

@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 Objects.equals(id, publisher.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
@@ -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<Author, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<Book, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<Publisher, Long> {
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.h2.console.enabled=true
24 changes: 24 additions & 0 deletions src/main/resources/templates/authors/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Framework Guru</title>
</head>
<body>
<h1>Author List</h1>

<table>
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<tr th:each="author : ${authors}">
<td th:text="${author.id}">123</td>
<td th:text="${author.name}">Joe</td>
<td th:text="${author.lastName}">Smith</td>
</tr>
</table>

</body>
</html>
24 changes: 24 additions & 0 deletions src/main/resources/templates/books/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring Framework Guru</title>
</head>
<body>
<h1>Book List</h1>

<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
</tr>
<tr th:each="book : ${books}">
<td th:text="${book.id}">123</td>
<td th:text="${book.title}"> Spring in Action</td>
<td th:text="${book.publisher.name}">Wrox</td>
</tr>
</table>

</body>
</html>