Environment build
This article uses the H2 memory database to demonstrate the use of Spring Data JPA.
Import JPA and H2 dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Configure the data source in application.yml:
spring:
sql:
init:
platform: h2
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:myh2
jpa:
hibernate:
ddl-auto: update
- The data source of H2 is configured here
- The implementation of JPA used here is Hibernate. The purpose of configuring "ddl-auto: update" is to automatically check whether the database structure and its tables match the entity (Entity) class configured by Hibernate when starting the application. If it does not match, it will Update the database schema to reflect the entity class changes.
Note: Try not to configure ddl-auto as update in a formal environment.