- Spring Boot 2.0 Cookbook(Second Edition)
- Alex Antonov
- 356字
- 2021-06-24 19:24:38
How to do it...
- Create a new package folder named entity under the src/main/java/com/example/bookpub directory from the root of our project.
- In this newly created package, create a new class named Book with the following content:
@Entity public class Book { @Id @GeneratedValue private Long id; private String isbn; private String title; private String description; @ManyToOne private Author author; @ManyToOne private Publisher publisher; @ManyToMany private List<Reviewers> reviewers; protected Book() {} public Book(String isbn, String title, Author author,
Publisher publisher) { this.isbn = isbn; this.title = title; this.author = author; this.publisher = publisher; } //Skipping getters and setters to save space, but we do need them }
- As any book should have an author and a publisher, and ideally some reviewers, we need to create these entity objects as well. Let's start by creating an Author entity class, under the same directory as our Book, as follows:
@Entity public class Author { @Id @GeneratedValue private Long id; private String firstName; private String lastName; @OneToMany(mappedBy = "author") private List<Book> books; protected Author() {} public Author(String firstName, String lastName) {...} //Skipping implementation to save space, but we do need
it all }
- Similarly, we will create the Publisher and Reviewer classes, as shown in the following code:
@Entity public class Publisher { @Id @GeneratedValue private Long id; private String name; @OneToMany(mappedBy = "publisher") private List<Book> books; protected Publisher() {} public Publisher(String name) {...} } @Entity public class Reviewer { @Id @GeneratedValue private Long id; private String firstName; private String lastName; protected Reviewer() {} public Reviewer(String firstName, String lastName)
{...}
}
- Now we will create our BookRepository interface by extending Spring's CrudRepository interface under the src/main/java/com/example/bookpub/repository package, as follows:
@Repository public interface BookRepository
extends CrudRepository<Book, Long> { public Book findBookByIsbn(String isbn); }
- Finally, let's modify our StartupRunner class in order to print the number of books in our collection, instead of some random datasource string, by autowiring a newly created BookRepository and printing the result of a .count() call to the log, as follows:
public class StartupRunner implements CommandLineRunner { @Autowired private BookRepository bookRepository; public void run(String... args) throws Exception { logger.info("Number of books: " +
bookRepository.count()); } }
推薦閱讀
- 數據庫基礎與應用:Access 2010
- Test-Driven Development with Mockito
- SQL Server 2008數據庫應用技術(第二版)
- WS-BPEL 2.0 Beginner's Guide
- The Game Jam Survival Guide
- SQL Server深入詳解
- Visual FoxPro數據庫技術基礎
- Access 2010數據庫程序設計實踐教程
- 云原生架構:從技術演進到最佳實踐
- 數據之美:一本書學會可視化設計
- 大數據技術體系詳解:原理、架構與實踐
- 掌中寶:電腦綜合應用技巧
- 推薦系統全鏈路設計:原理解讀與業務實踐
- SQL進階教程(第2版)
- 從零進階!數據分析的統計基礎(第2版)