One, MAVEN relies on
<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>
Second, the configuration file application.properties
Spring.datasource.driver-Class-Name=Com.mysql.jdbc.Driver Spring.datasource.url=Jdbc:mysql://localhost3306/sbtest Spring.datasource.username=Root Spring.datasource.password=1234SPRING.JPA.Database=MySQL spring.jpa.show-Sql=true Spring.jpa.hibernate.ddl-Auto=UpdateSpring.jpa.hibernate.naming.strategy=Org.hibernate.cfg.ImprovedNamingStrategy
Iii. establishment of entity and repository
1. Book Entity class
Packagecom.casic.entity; ImportJavax.persistence.*;
Importjava.util.List; @Entity Public classBook {@Id @GeneratedValuePrivateLong ID; PrivateString ISBN; PrivateString title; PrivateString description; @ManyToOnePrivateAuthor Author; @ManyToOnePrivatePublisher publisher; @ManyToManyPrivateList<reviewer>reviewers; protectedBook () {} PublicBook (Author Author, String ISBN, Publisher publisher, string title) { This. Author =author; This. ISBN =ISBN; This. Publisher =publisher; This. title =title; }
}
2. Author entity class
Packagecom.casic.entity; ImportJavax.persistence.*;
Importjava.util.List; @Entity Public classAuthor {@Id @GeneratedValuePrivateLong ID; PrivateString FirstName; PrivateString LastName; @OneToMany (Mappedby= "Author")
PrivateList<book>Books; protectedAuthor () {} PublicAuthor (String firstName, String lastName) { This. FirstName =FirstName; This. LastName =LastName; }
}
3. Publisher entity class
Packagecom.casic.entity; ImportJavax.persistence.*;
Importjava.util.List; @Entity Public classPublisher {@Id @GeneratedValuePrivateLong ID; PrivateString name; @OneToMany (Mappedby= "Publisher")
PrivateList<book>Books; protectedPublisher () {} PublicPublisher (String name) { This. Name =name; }
}
4, Reviewer
Packagecom.casic.entity;ImportJavax.persistence.*; @Entity Public classReviewer {@Id @GeneratedValuePrivateLong ID; PrivateString FirstName; PrivateString LastName; protectedReviewer () {} PublicReviewer (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;
@Repository
publicinterfaceextends Crudrepository<book, long> {book
findbookbyisbn (String ISBN);
}
6. Mystartuprunner Command Line startup class
PackageCom.casic.service; Importcom.casic.repository.BookRepository;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.boot.CommandLineRunner;Importorg.springframework.stereotype.Component; /*** Service startup execution *@authorOftenlin*/@Component Public classMystartuprunnerImplementsCommandlinerunner {@AutowiredPrivatebookrepository bookrepository; @Override Public voidRun (String ... args)throwsException {System.out.print ("Book Count:" +Bookrepository.count ()); }
}
7. Start class MyApp
PackageCom.casic; Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;ImportOrg.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; @SpringBootApplication Public classmyapp{ Public Static voidMain (String args[]) {Springapplication.run (MyApp).class, args); }
}
MAVEN builds the spring boot + MySQL Foundation project