maven 構建spring boot + mysql 的基礎項目

來源:互聯網
上載者:User

標籤:user   weight   localhost   logs   rcu   figure   run   factor   命令   

一、maven 依賴

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.1.RELEASE</version>        <relativePath /> <!-- lookup parent from repository -->    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-jpa</artifactId>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <scope>runtime</scope>        </dependency>    </dependencies>

二、設定檔 application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/sbtestspring.datasource.username=rootspring.datasource.password=1234spring.jpa.database=mysqlspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy

三、建立Entity和Repository

1、Book 實體類

package com.casic.entity;import javax.persistence.*;import java.util.List;@Entitypublic 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<Reviewer> reviewers;    protected Book() { }    public Book(Author author, String isbn, Publisher publisher, String title) {        this.author = author;        this.isbn = isbn;        this.publisher = publisher;        this.title = title;    }}

2、Author 實體類

package com.casic.entity;import javax.persistence.*;import java.util.List;@Entitypublic 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) {        this.firstName = firstName;        this.lastName = lastName;    }}

3、Publisher 實體類

package com.casic.entity;import javax.persistence.*;import java.util.List;@Entitypublic class Publisher {    @Id    @GeneratedValue    private Long id;    private String name;    @OneToMany(mappedBy = "publisher")    private List<Book> books;    protected Publisher() { }    public Publisher(String name) {        this.name = name;    }}

4、Reviewer

package com.casic.entity;import javax.persistence.*;@Entitypublic class Reviewer {    @Id    @GeneratedValue    private Long id;    private String firstName;    private String lastName;    protected Reviewer() { }    public Reviewer(String firstName, String lastName) {        this.firstName = firstName;        this.lastName = lastName;    }}

5、BookRepository 

package com.casic.repository;import com.casic.entity.Book;        import org.springframework.data.repository.CrudRepository;        import org.springframework.stereotype.Repository;@Repositorypublic interface BookRepository extends CrudRepository<Book, Long> {    Book findBookByIsbn(String isbn);}

6、MyStartupRunner 命令列啟動類

package com.casic.service;import com.casic.repository.BookRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;/** * 服務啟動執行 * @author   oftenlin */@Componentpublic class MyStartupRunner implements CommandLineRunner {    @Autowired    private BookRepository bookRepository;    @Override    public void run(String... args) throws Exception {        System.out.print("book count:"+bookRepository.count());    }}

7、啟動類 MyApp 

package com.casic;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;@SpringBootApplicationpublic class MyApp{    public static void main(String args[]) {        SpringApplication.run(MyApp.class, args);    }}

 

maven 構建spring boot + mysql 的基礎項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.