- 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()); } }
推薦閱讀
- 數據產品經理高效學習手冊:產品設計、技術常識與機器學習
- Greenplum:從大數據戰略到實現
- 數據庫技術與應用教程(Access)
- InfluxDB原理與實戰
- 大數據:從概念到運營
- 大數據營銷:如何讓營銷更具吸引力
- Remote Usability Testing
- Visual Studio 2013 and .NET 4.5 Expert Cookbook
- 大數據數學基礎(Python語言描述)
- 算法設計與分析
- 云工作時代:科技進化必將帶來的新工作方式
- SQL Server 2012 數據庫教程(第3版)
- ORACLE 11g權威指南
- 算法設計與問題求解(第2版):計算思維培養
- TypeScript Microservices