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
import
javax.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.)