Rename data-jdbc to data-jdbc-h2
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.example.data.jdbc;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.example.data.jdbc.model.Author;
|
||||
|
||||
import org.springframework.data.jdbc.repository.query.Query;
|
||||
import org.springframework.data.repository.ListCrudRepository;
|
||||
|
||||
public interface AuthorRepository extends ListCrudRepository<Author, Long> {
|
||||
|
||||
Optional<Author> findByNameContainingIgnoreCase(String partialName);
|
||||
|
||||
@Query("SELECT * FROM author a WHERE a.name = :name LIMIT 1")
|
||||
Optional<Author> queryFindByName(String name);
|
||||
|
||||
}
|
||||
81
data-jdbc-h2/src/main/java/com/example/data/jdbc/CLR.java
Normal file
81
data-jdbc-h2/src/main/java/com/example/data/jdbc/CLR.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package com.example.data.jdbc;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.example.data.jdbc.model.Author;
|
||||
import com.example.data.jdbc.model.Book;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class CLR implements CommandLineRunner {
|
||||
|
||||
private final AuthorRepository authorRepository;
|
||||
|
||||
CLR(AuthorRepository authorRepository) {
|
||||
this.authorRepository = authorRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
insertAuthors();
|
||||
listAllAuthors();
|
||||
findById();
|
||||
findByPartialName();
|
||||
queryFindByName();
|
||||
deleteAll();
|
||||
}
|
||||
|
||||
private void deleteAll() {
|
||||
this.authorRepository.deleteAll();
|
||||
long count = this.authorRepository.count();
|
||||
System.out.printf("deleteAll(): count = %d%n", count);
|
||||
}
|
||||
|
||||
private void queryFindByName() {
|
||||
Author author1 = this.authorRepository.queryFindByName("Josh Long").orElse(null);
|
||||
Author author2 = this.authorRepository.queryFindByName("Martin Kleppmann").orElse(null);
|
||||
|
||||
System.out.printf("queryFindByName(): author1 = %s%n", author1);
|
||||
System.out.printf("queryFindByName(): author2 = %s%n", author2);
|
||||
}
|
||||
|
||||
private void findByPartialName() {
|
||||
Author author1 = this.authorRepository.findByNameContainingIgnoreCase("sh lo").orElse(null);
|
||||
Author author2 = this.authorRepository.findByNameContainingIgnoreCase("in kl").orElse(null);
|
||||
|
||||
System.out.printf("findByPartialName(): author1 = %s%n", author1);
|
||||
System.out.printf("findByPartialName(): author2 = %s%n", author2);
|
||||
}
|
||||
|
||||
private void findById() {
|
||||
Author author1 = this.authorRepository.findById(1L).orElse(null);
|
||||
Author author2 = this.authorRepository.findById(2L).orElse(null);
|
||||
|
||||
System.out.printf("findById(): author1 = %s%n", author1);
|
||||
System.out.printf("findById(): author2 = %s%n", author2);
|
||||
}
|
||||
|
||||
private void listAllAuthors() {
|
||||
List<Author> authors = this.authorRepository.findAll();
|
||||
for (Author author : authors) {
|
||||
System.out.printf("listAllAuthors(): author = %s%n", author);
|
||||
for (Book book : author.getBooks()) {
|
||||
System.out.printf("\t%s%n", book);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void insertAuthors() {
|
||||
Author author1 = this.authorRepository.save(new Author(null, "Josh Long",
|
||||
Set.of(new Book(null, "Reactive Spring"), new Book(null, "Cloud Native Java"))));
|
||||
Author author2 = this.authorRepository.save(
|
||||
new Author(null, "Martin Kleppmann", Set.of(new Book(null, "Designing Data Intensive Applications"))));
|
||||
|
||||
System.out.printf("insertAuthors(): author1 = %s%n", author1);
|
||||
System.out.printf("insertAuthors(): author2 = %s%n", author2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.data.jdbc;
|
||||
|
||||
import org.springframework.aot.smoketest.thirdpartyhints.HikariRuntimeHints;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ImportRuntimeHints;
|
||||
|
||||
@SpringBootApplication
|
||||
@ImportRuntimeHints(HikariRuntimeHints.class)
|
||||
public class DataJdbcApplication {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
SpringApplication.run(DataJdbcApplication.class, args);
|
||||
Thread.currentThread().join(); // To be able to measure memory consumption
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.data.jdbc;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
||||
|
||||
@Configuration
|
||||
@EnableJdbcRepositories
|
||||
class DataJdbcConfiguration {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.example.data.jdbc.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Author {
|
||||
|
||||
@Id
|
||||
private final Long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Set<Book> books;
|
||||
|
||||
public Author(Long id, String name, Set<Book> books) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.books = books;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<Book> getBooks() {
|
||||
return 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(name, author.name)
|
||||
&& Objects.equals(books, author.books);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, books);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Author{" + "name='" + name + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.example.data.jdbc.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
private final Long id;
|
||||
|
||||
private final String title;
|
||||
|
||||
public Book(Long id, String title) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@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) && Objects.equals(title, book.title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book{" + "title='" + title + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user