Open In App
Related Articles

Spring Data JPA – Find Records From MySQL

Improve Article
Save Article
Like Article

Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Java persistence API is like an interface that contains most of the methods that are used for data manipulation in the MySQL table and hibernate is the implementation of these methods, So we don’t have to create any method for inserting the data in the MySQL. In this article, we will discuss how to find the particular record from the MySQL table using the primary key.

JPARepositery<>.findById() method is used for finding the records from mysql.

Step By Step Implementation

Go to Spring Initializr Fill in the details as per the requirements. Click on Generate which will download the starter project. Extract the zip file.



<?xml version="1.0" encoding="UTF-8"?>
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>



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



Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project. For database operation we have to configure the Spring application with the database also it is required to add configuration of the database before executing the Spring project. All the configuration is added in the file of the Spring project. file:




Project Structure: (Table structure of the MySQL table)



public class User {
    @GeneratedValue(strategy = GenerationType.AUTO)
    int id;
    String name;
    User() {}
    User(int id, String name)
    { = id; = name;



UserRepo:(This file is used for extending the JPA methods with hibernate implementation)



interface UserRepo extends JpaRepository<User,Integer> {






package com.example.SpringBootApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Optional;
public class SpringBootAppApplication implements  CommandLineRunner {
    UserRepo ob;
    public static void main(String[] args) {, args);
    public void run(String... args) throws Exception {
        User record=new User(1,"Aayush");;
        Optional<User> record=ob.findById(1);>{
            System.out.print(" ";



Run the main application:



Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 21 Feb, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials