2.3.1 聚焦应用的功能

  有一种方式可以让我们了解SpringBoot自动配置。在接下来的几页的叙述中,会像你展示配置的时候如果不使用SpringBoot是怎样的。可是已经有很多不错关于Spring的书涉及这一点,所以再将这些配置展现一边不会是我们的readlist应用开发的更快。
  不在浪费时间来讨论Spring的配置了,我们知道SpringBoot会帮我们很好的管好这一切。那就让我们看看使用SpringBoot的自动配置能让我们专注于readlist应用代码编写的优点是什么吧。

定义领域模型(domain)
  我们的应用中主要的领域模型是书籍信息,将其使用在读者阅读列表上。因此,我们需要定义一个实体类来表述书籍信息。正如代码清单2-5所示代码一样:


代码清单 2.5 The Book class represents a book in the reading list

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
      private Long id;
      private String reader;
      private String isbn;
      private String title;
      private String author;
      private String description;
      public Long getId() {
          return id;
      }
      public void setId(Long id) {
          this.id = id;
      }
      public String getReader() {
          return reader;
      }
      public void setReader(String reader) {
          this.reader = reader;
      }
      public String getIsbn() {
          return isbn;
      }
      public void setIsbn(String isbn) {
          this.isbn = isbn;
      }
      public String getTitle() {
          return title;
      }
      public void setTitle(String title) {
          this.title = title;
      }
      public String getAuthor() {
          return author;
      }
      public void setAuthor(String author) {
          this.author = author;
      }
      public String getDescription() {
          return description;
      }
      public void setDescription(String description) {
          this.description = description;
      }

  正如你所见,Book类是一个简单Java类,又一堆描述book的属性和必要的访问方法。使用@Entity注解指名是一个JPA的实体。id属性上注解@Id和@GeneratedValue来表示了这个域是实体的唯一标识并且它的值会自动递增。

定义repository的接口
Next up, we need to define the repository through which the ReadingList objects will be persisted to the database. Because we’re using Spring Data JPA , that task is a simple matter of creating an interface that extends Spring Data JPA ’s JpaRepository interface:


import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ReadingListRepository extends JpaRepository<Book, Long> {
List<Book> findByReader(String reader);
}

By extending JpaRepository , ReadingListRepository inherits 18 methods for per- forming common persistence operations. The JpaRepository interface is parameter- ized with two parameters: the domain type that the repository will work with, and the type of its ID property. In addition, I’ve added a findByReader() method through which a reading list can be looked up given a reader’s username.

If you’re wondering about who will implement ReadingListRepository and the 18 methods it inherits, don’t worry too much about it. Spring Data provides a special magic of its own, making it possible to define a repository with just an interface. The interface will be implemented automatically at runtime when the application is started.

CREATING THE WEB INTERFACE Now that we have the application’s domain defined and a repository for persisting objects from that domain to the database, all that’s left is to create the web front-end. A Spring MVC controller like the one in listing 2.6 will handle HTTP requests for the application.

results matching ""

    No results matching ""