What JPA
Java Persistence API: An API for object persistence
The Java EE 5.0 Platform Standard ORM specification allows applications to access the persistence layer in a uniform way
The relationship between JPA and hibernate can simply be understood as JPA is the standard interface, and Hibernate is implemented.
Integrated JPA Implementation of the book query small case
Springboot with 2.0.0 version
1. Import related dependencies
1 <!--Web Apps -2 <Dependency>3 <groupId>Org.springframework.boot</groupId>4 <Artifactid>Spring-boot-starter-web</Artifactid>5 </Dependency>6 7 <!--Test -8 <Dependency>9 <groupId>Org.springframework.boot</groupId>Ten <Artifactid>Spring-boot-starter-test</Artifactid> One <Scope>Test</Scope> A </Dependency> - - <!--Freemarker Support - the <Dependency> - <groupId>Org.springframework.boot</groupId> - <Artifactid>Spring-boot-starter-freemarker</Artifactid> - </Dependency> + - <!--MySQL driver - + <Dependency> A <groupId>Mysql</groupId> at <Artifactid>Mysql-connector-java</Artifactid> - </Dependency> - - <!--Integrated JPA - - <Dependency> - <groupId>Org.springframework.boot</groupId> in <Artifactid>Spring-boot-starter-data-jpa</Artifactid> - </Dependency>
2.application.properties
1 Spring.datasource.url=jdbc:mysql://localhost:3306/bookshop 2 Spring.datasource.username=root 3 spring.datasource.password=123 4 Spring.datasource.driver-class-name=com.mysql.jdbc.driver
3.entity Layer
1@Entity (name = "book")2@JsonIgnoreProperties (value={"Hibernatelazyinitializer", "handler", "Fieldhandler"})3 Public classBook {4 @Id5 @GeneratedValue6 PrivateInteger BookID;7 @Column8 PrivateString bookname;9 @ColumnTen PrivateInteger Bookprice; One A The get Set method is omitted: -}
4.dao Layer Inheritance Jparepository
1 Public Interface extends Jparepository<book,integer> {2 }
5.controller Layer
1 @Controller2 Public classBookcontroller {3 @Autowired4 PrivateIbookdao Bookdao;5 6@RequestMapping ("/getbookbyid")7 @ResponseBody8 PublicBook Getjpabookbyid (Integer bookid) {9 returnBookdao.getone (bookid);Ten } One}
6. Start the project
Error Resolution:
1. Need to add @responsebody annotations on the Controller Layer Query method, parsing to JSON format after the addition or error ...
Add @jsonignoreproperties (value={"Hibernatelazyinitializer", "handler", "Fieldhandler") to the entity class On-line query function is to ignore some properties to JSON format
To this integrated JPA end ....
Springboot Integrated JPA