|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.example.data.jpa; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import com.example.data.jpa.model.Author; |
| 23 | +import com.example.data.jpa.model.Book; |
| 24 | + |
| 25 | +import org.springframework.boot.CommandLineRunner; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | +import org.springframework.transaction.annotation.Transactional; |
| 28 | + |
| 29 | +@Component |
| 30 | +class CLR implements CommandLineRunner { |
| 31 | + |
| 32 | + private final AuthorRepository authorRepository; |
| 33 | + |
| 34 | + CLR(AuthorRepository authorRepository) { |
| 35 | + this.authorRepository = authorRepository; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + @Transactional |
| 40 | + public void run(String... args) { |
| 41 | + var authors = insertAuthors(); |
| 42 | + listAllAuthors(); |
| 43 | + findById(authors); |
| 44 | + findByPartialName(); |
| 45 | + queryFindByName(); |
| 46 | + deleteAll(); |
| 47 | + } |
| 48 | + |
| 49 | + private void deleteAll() { |
| 50 | + this.authorRepository.deleteAll(); |
| 51 | + long count = this.authorRepository.count(); |
| 52 | + System.out.printf("deleteAll(): count = %d%n", count); |
| 53 | + } |
| 54 | + |
| 55 | + private void queryFindByName() { |
| 56 | + Author author1 = this.authorRepository.queryFindByName("Josh Long").orElse(null); |
| 57 | + Author author2 = this.authorRepository.queryFindByName("Martin Kleppmann").orElse(null); |
| 58 | + |
| 59 | + System.out.printf("queryFindByName(): author1 = %s%n", author1); |
| 60 | + System.out.printf("queryFindByName(): author2 = %s%n", author2); |
| 61 | + } |
| 62 | + |
| 63 | + private void findByPartialName() { |
| 64 | + Author author1 = this.authorRepository.findByNameContainingIgnoreCase("sh lo").orElse(null); |
| 65 | + Author author2 = this.authorRepository.findByNameContainingIgnoreCase("in kl").orElse(null); |
| 66 | + |
| 67 | + System.out.printf("findByPartialName(): author1 = %s%n", author1); |
| 68 | + System.out.printf("findByPartialName(): author2 = %s%n", author2); |
| 69 | + } |
| 70 | + |
| 71 | + private void findById(List<Author> authors) { |
| 72 | + Author author1 = this.authorRepository.findById(authors.get(0).getId()).orElse(null); |
| 73 | + Author author2 = this.authorRepository.findById(authors.get(1).getId()).orElse(null); |
| 74 | + |
| 75 | + System.out.printf("findById(): author1 = %s%n", author1); |
| 76 | + System.out.printf("findById(): author2 = %s%n", author2); |
| 77 | + } |
| 78 | + |
| 79 | + private void listAllAuthors() { |
| 80 | + List<Author> authors = this.authorRepository.findAll(); |
| 81 | + for (Author author : authors) { |
| 82 | + System.out.printf("listAllAuthors(): author = %s%n", author); |
| 83 | + for (Book book : author.getBooks()) { |
| 84 | + System.out.printf("\t%s%n", book); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private List<Author> insertAuthors() { |
| 90 | + Author author1 = this.authorRepository.save(new Author(null, "Josh Long", |
| 91 | + Set.of(new Book(null, "Reactive Spring"), new Book(null, "Cloud Native Java")))); |
| 92 | + Author author2 = this.authorRepository.save( |
| 93 | + new Author(null, "Martin Kleppmann", Set.of(new Book(null, "Designing Data Intensive Applications")))); |
| 94 | + |
| 95 | + System.out.printf("insertAuthors(): author1 = %s%n", author1); |
| 96 | + System.out.printf("insertAuthors(): author2 = %s%n", author2); |
| 97 | + |
| 98 | + return Arrays.asList(author1, author2); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments