บทนำสู่ Spring Data JPA
ทำความเข้าใจประโยชน์ของ Spring Data JPA และตั้งค่าการเชื่อมต่อฐานข้อมูลสำหรับแอปพลิเคชัน
บทนำสู่ Spring Data JPA เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 6 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 6 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Data Persistence?
Imagine an application that needs to remember user preferences, product details, or blog posts. If this data disappears every time the app closes, it's not very useful!
Data persistence is the ability for an application to store data permanently. This means the data survives even after the application stops running or the device is turned off. Databases are the most common tools for achieving this.
Java & Database Interaction
In Java, interacting with databases traditionally involves JDBC (Java Database Connectivity), a low-level API. While powerful, it can be quite verbose.
To simplify things, the JPA (Java Persistence API) standard emerged. JPA provides an ORM (Object-Relational Mapping) approach, allowing you to work with database tables using regular Java objects, reducing direct SQL interaction.
Meet Spring Data JPA!
Spring Data JPA is a powerful module within the larger Spring Data project. It builds on top of the JPA standard to make database access even simpler and more efficient in Spring applications.
Its primary goal is to significantly reduce the boilerplate code required for common data access operations, letting you focus on your application's business logic.
Why Spring Data JPA is Great
Spring Data JPA offers several compelling benefits:
- Reduced Boilerplate: You don't need to write basic CRUD (Create, Read, Update, Delete) methods.
- Automatic Query Generation: Define method names (e.g.,
findByEmail), and Spring Data JPA automatically generates the SQL query. - Seamless Spring Integration: It integrates perfectly with other Spring features like dependency injection and transaction management.
- Support for Various Databases: Works with popular relational databases like H2, MySQL, PostgreSQL, and Oracle.
Essential Dependencies
To use Spring Data JPA in a Spring Boot project, you need to add specific dependencies to your pom.xml (for Maven) or build.gradle (for Gradle).
The two main ones we'll use for a basic setup are:
spring-boot-starter-data-jpa: This is the core dependency. It brings in JPA, Hibernate (the default JPA implementation), and Spring Data JPA itself.h2: An in-memory database. It's excellent for development and testing because it's lightweight and easy to set up, without needing a separate database server.
Configuring `pom.xml`
Here's how you'd typically add these dependencies to your pom.xml file. The spring-boot-starter-parent manages versions for you.
Notice the <scope>runtime</scope> for H2, meaning it's needed during execution but not for compiling your code.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.coddykit</groupId>
<artifactId>jpa-intro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jpa-intro</name>
<description>Intro to Spring Data JPA</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Database Connection Auto-Config
One of Spring Boot's most convenient features is auto-configuration. When it detects the spring-boot-starter-data-jpa and a database driver (like H2) on the classpath, it automatically configures a DataSource.
A DataSource is essentially how your application connects to the database. This means you often don't need to manually write connection pooling or database setup code; Spring Boot handles sensible defaults for you!
`application.properties` for H2
While auto-configuration is great, you'll often want to customize database settings. This is typically done in application.properties or application.yml files.
Here's how to configure an in-memory H2 database and enable its built-in web console, which is super helpful for viewing your database contents during development:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2DialectOur First Spring Boot App
With the necessary dependencies and database configuration in place, our Spring Boot application is ready to start up and connect to the H2 database. This simple class launches the application.
After running, you can typically access the H2 console at http://localhost:8080/h2-console using the credentials configured above.
package com.coddykit.jpaintro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JpaIntroApplication {
public static void main(String[] args) {
SpringApplication.run(JpaIntroApplication.class, args);
System.out.println("Spring Boot application started with H2 database!");
// The application will keep running until manually stopped.
// If h2 console is enabled, you can visit:
// http://localhost:8080/h2-console
// Use JDBC URL: jdbc:h2:mem:testdb, User: sa, Password: password
}
}Quick Check: Core Dependency
Which of the following dependencies is primarily responsible for bringing in Spring Data JPA and its underlying JPA implementation (like Hibernate) into a Spring Boot project?
Recap: Intro to Spring Data JPA
Great job! In this lesson, you've taken your first steps into data persistence with Spring Boot.
- We understood why data persistence is crucial for applications.
- We introduced Spring Data JPA as a powerful tool to simplify database interactions.
- You learned about adding essential dependencies like
spring-boot-starter-data-jpaandh2. - We configured our in-memory H2 database using
application.propertiesand saw how Spring Boot auto-configures theDataSource. - Finally, you ran a basic Spring Boot application with H2 ready for action!
Next, we'll dive deeper into defining entities and repositories to truly interact with our database!
คำถามที่พบบ่อย
บทเรียน “บทนำสู่ Spring Data JPA” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บทนำสู่ Spring Data JPA” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 6 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บทนำสู่ Spring Data JPA”
ทำความเข้าใจประโยชน์ของ Spring Data JPA และตั้งค่าการเชื่อมต่อฐานข้อมูลสำหรับแอปพลิเคชัน คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 6 บทเรียน
บทเรียน “บทนำสู่ Spring Data JPA” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผสานฐานข้อมูล H2 และ JPA
- บทนำสู่ Spring Data JPA
- การสร้างเอนทิตีและคลังข้อมูล
- การกำหนดเอนทิตีและคลังข้อมูล
- การดำเนินการ CRUD ด้วย REST
- การดำเนินการ CRUD