SPRING-DATA-JPA of Springboot operation (I.)

Source: Internet
Author: User

Spring-data-jpa

The JPA (Java persistence API) defines a series of criteria for object persistence,

At present, the implementation of this specification of products have hibernate, TopLink and so on.

The spring data JPA framework, which focuses on the only business logic code that is not simplified to the spring, has been saved by the developer's only remaining implementation of the persistence layer's business logic, and the only thing to do is to declare the interface for the persistence layer, and the rest to spring Data JPA To help you finish!

Below we demonstrate the use of SPRING-DATA-JPA under Springboot

This post is a simple demonstration of configuration and automatic generation of tables

First step, introduction of JPA and MySQL driver support

Or the previous way into pom.xml,alt+/into the edit view

Choose JPA and MySQL

<dependency>

     < groupId >mysql</ groupId >      < artifactId >mysql-connector-java</ artifactId >      < scope >runtime</ scope > </ dependency > < dependency >      < groupId >org.springframework.boot</ groupId >      < artifactId >spring-boot-starter-data-jpa</ artifactId > </ dependency >

Next Configure Application.properties

Spring.datasource.driver-class-name=com.mysql.jdbc.driver

Spring.datasource.url=jdbc:mysql://localhost:3306/db_book

Spring.datasource.username=root

spring.datasource.password=123456

Spring.jpa.hibernate.ddl-auto=update

Spring.jpa.show-sql=true

Above is the configuration data source

Here's the configuration, like Ddl-auto, who learned hibernate, knows that we usually update the operation by using update

Show-sql is to display SQL statements

(Of course, we will find that the configuration of this type of properties is a bit redundant, and then we change to the mainstream yml form)

We're going to create a new db_book in the database.

Next, create a new book entity

importjavax.persistence.Column;

import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table (name= "t_book" ) public class Book {      @Id      @GeneratedValue      private Integer id;           @Column (length= 100 )      private String bookName;           public Integer getId() {          return id;      }      public void setId(Integer id) {          this .id = id;      }      public String getBookName() {          return bookName;      }      public void setBookName(String bookName) {          this .bookName = bookName;      }           }

OK, that's it, we're starting the Helloworldapplication class.

Automatically build a table after you start the database

SPRING-DATA-JPA of Springboot operation (I.)

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.