Skip to content
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

My sql integration #271

Open
wants to merge 6 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package guru.springframework.spring5webapp.bootstrap;

import guru.springframework.spring5webapp.domain.Book;
import guru.springframework.spring5webapp.repositories.BookRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
* Created by João Nunes on 12/06/2023.
*/
@Component
public class DataInitializer implements CommandLineRunner {

private final BookRepository bookRepository;

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


@Override
public void run(String... args) throws Exception {
Book bookDDD = new Book("Domain Driven Design", "123", "RandomHouse");

System.out.println("Id: " + bookDDD.getId());

Book savedDDD = bookRepository.save(bookDDD);

System.out.println("Id: " + savedDDD.getId());

Book bookSIA = new Book("Spring in Action", "123", "RandomHouse");

Book savedSIA = bookRepository.save(bookSIA);

bookRepository.findAll().forEach(book -> {
System.out.println("Book Id: " + book.getId());
System.out.println("Book Title: " + book.getTitle());
});
}
}
73 changes: 73 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,73 @@
package guru.springframework.spring5webapp.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;

@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String isbn;
private String publisher;

public Book() {
}

@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 id != null ? id.hashCode() : 0;
}

public Book(String title, String isbn, String publisher) {
this.title = title;
this.isbn = isbn;
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 String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.domain.Book;
import org.springframework.data.jpa.repository.JpaRepository;


// I could also have used the annotations
public interface BookRepository extends JpaRepository<Book, Long> {
}
16 changes: 16 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#spring.jpa.show-sql=true

#Show SQL
spring.jpa.properties.hibernate.show_sql=true

#Format SQL
spring.jpa.properties.hibernate.format_sql=true

#Show bind values
logging.level.org.hibernate.type.descriptor.sql=trace

spring.h2.console.enabled=true




Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package guru.springframework.spring5webapp;

import guru.springframework.spring5webapp.repositories.BookRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Spring5webappApplicationTests {

@Test
public void contextLoads() {
}
@Autowired
BookRepository bookRepository;

@Test
public void contextLoads() {
long count = bookRepository.count();
assertThat(count).isGreaterThan(0);

}

}