VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/spring-data-jpa-find-records-from-mysql/

⇱ Spring Data JPA - Find Records From MySQL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Data JPA - Find Records From MySQL

Last Updated : 23 Apr, 2026

Spring Data JPA simplifies database operations in Spring Boot applications by reducing boilerplate code and providing built-in methods for data access. In this article, we will learn how to retrieve records from a MySQL database using the findById() method, which fetches data based on the primary key and returns an Optional for safe null handling.

  • findById() is a predefined method of JpaRepository
  • No need to write custom SQL queries

Step-by-Step Implementation

Follow these steps to create, configure, and run a Spring Boot application that retrieves records from a MySQL database using findById() method in Spring Data Jpa.

Step 1: Create a Spring Boot Project

Go to Spring Initializr and generate a Spring Boot project with the following dependencies:

  • Spring Web
  • Spring Data JPA
  • MySQL Driver
👁 Spring-Initializr

Step 2: Open project on IDE

Extract the zip file. Now open a suitable IDE and then go to File -> New -> Project from Existing Sources and select pom.xml. Click on import changes on prompt and wait for the project to sync.

👁 Image

Note: Ensure the same JDK version is selected while importing the Maven project. Also, configure the database connection in application.properties before running the application.

Step 3: Add Required Dependencies

Modify the pom.xml file to include the necessary dependencies:

Step 4: Configure Database Connection

  • All required configurations should be specified in the application.properties file of the Spring project.
  • Add the following properties in src/main/resources/application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/userdb
spring.datasource.username=root # replace with your MySQL username
spring.datasource.password=yourpassword # replace with your MySQL password
spring.jpa.hibernate.ddl-auto=update

Project Directory Structure:

👁 Project-Structure

Step 5: Create the Entity Class

Create a User entity in src/main/java/com/example/model/User.java:

Step 6: Create Repository Interface

Define a repository interface in src/main/java/com/example/repository/UserRepository.java:

Step 7: Implement Service Class

Create a service to interact with the database in src/main/java/com/example/service/UserService.java:

Step 8: Create Controller for API

Implement a controller to handle HTTP requests in src/main/java/com/example/controller/UserController.java:

Step 9: Run the Application 

Start the Spring Boot application using:

mvn spring-boot:run

Output:

👁 Image


Once the application is running, test the API using Postman or a browser:

GET http://localhost:8080/users/1 

Expected Output (JSON Response):

{
"id": 1,
"name": "Aayush"
}

Comment
Article Tags:

Explore