Spring Data JPA练习

1、Entity

@Entity

@Data

public class Bookimplements Serializable{

@Id

@GeneratedValue

    private long id;

    private StringbookName;

    private StringbookAuther;

    private StringbookIntroduce;

    private StringbookCover;

}

2、DAO

public interface BookRepositoryextends JpaRepository {

}

3、service

public interface BookService {

/**

* 获取所有 Book

*/

    ListfindAll();

    /**

* 获取 Book

*

    * @param id 编号

*/

    BookfindById(Long id);

}


@Service

public class BookServiceImplimplements BookService {

@Autowired

    BookRepositorybookRepository;

    @Override

    public ListfindAll() {

return bookRepository.findAll();

    }

@Override

    public BookfindById(Long id) {

return bookRepository.findById(id).get();

    }

}

4、Controller

@Service

public class BookServiceImplimplements BookService {

@Autowired

    BookRepositorybookRepository;

    @Override

    public ListfindAll() {

return bookRepository.findAll();

    }

@Override

    public BookfindById(Long id) {

return bookRepository.findById(id).get();

    }

}

5、index

   

你可能感兴趣的:(Spring Data JPA练习)