Springboot Integrated JPA

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.