Skip to content

spring boot h2-console #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package guru.springframework.spring5webapp.bootstrap;

import guru.springframework.spring5webapp.repositories.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/authorsList";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


@Component
public class BootStrapData implements CommandLineRunner {
private final AuthorRepository authorRepository;
private final BookRepository bookRepository;
private 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 {
Author author1 = new Author("Ahmed", "Yamir");
Book book1 = new Book("Spring boot", "12QS");

author1.getBooks().add(book1);
book1.getAuthors().add(author1);

authorRepository.save(author1);
bookRepository.save(book1);

Author author2 = new Author("Taha", "Salhi");
Book book2 = new Book("C++", "AZ321GFF");

author2.getBooks().add(book2);
book2.getAuthors().add(author2);

authorRepository.save(author2);
bookRepository.save(book2);



Publisher publisger=new Publisher("Publisger 1","AddressL1","Ottawa","Ontario","K3E 6T9");
book1.setPublisher(publisger);
publisger.getBooks().add(book1);

book2.setPublisher(publisger);
publisger.getBooks().add(book2);

publisherRepository.save(publisger);

System.out.println("Number of book here: " + bookRepository.count());
System.out.println("Number of author here: " + authorRepository.count());
System.out.println("Number of publisher here: " + publisherRepository.count());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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;

@Controller
public class BookControllers {

private final BookRepository bookRepository;

public BookControllers(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}

@RequestMapping("/books")
public String getBooks(Model model){
model.addAttribute("books",bookRepository.findAll());
return "books/list";
}
}
80 changes: 80 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/model/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package guru.springframework.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
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<Book> 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<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> 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 Objects.equals(id, author.id) && Objects.equals(firstName, author.firstName) && Objects.equals(lastName, author.lastName) && Objects.equals(books, author.books);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

@Override
public String toString() {
return "Author{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", books=" + books +
'}';
}
}

92 changes: 92 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package guru.springframework.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
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;

public Publisher getPublisher() {
return publisher;
}

public void setPublisher(Publisher publisher) {
this.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() {
}

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

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<Author> getAuthors() {
return authors;
}

public void setAuthors(Set<Author> 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 Objects.equals(id, book.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", isbn='" + isbn + '\'' +
", authors=" + authors +
'}';
}
}
113 changes: 113 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/model/Publisher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package guru.springframework.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
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<Book> books=new HashSet<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Set<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> books) {
this.books = books;
}

public Publisher() {
}

public Publisher(String name, String addressLine1, String city, String state, String zip) {
this.name = name;
AddressLine1 = addressLine1;
this.city = city;
this.state = state;
this.zip = zip;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddressLine1() {
return AddressLine1;
}

public void setAddressLine1(String addressLine1) {
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 Objects.equals(name, publisher.name) && Objects.equals(AddressLine1, publisher.AddressLine1) && Objects.equals(city, publisher.city) && Objects.equals(state, publisher.state) && Objects.equals(zip, publisher.zip);
}

@Override
public int hashCode() {
return Objects.hash(name, AddressLine1, city, state, zip);
}

@Override
public String toString() {
return "Puplisher{" +
"name='" + name + '\'' +
", AddressLine1='" + AddressLine1 + '\'' +
", city='" + city + '\'' +
", state='" + state + '\'' +
", zip='" + zip + '\'' +
'}';
}
}
Loading