Spring Data JPA Insert Data in MySQL Table (original) (raw)

Last Updated : 12 Nov, 2025

Spring Data JPA simplifies database operations in Spring Boot by eliminating boilerplate code. It provides built-in methods to perform CRUD (Create, Read, Update, Delete) operations on relational databases like MySQL.

In this article, we will learn how to insert data into a MySQL database using Spring Boot, Spring Data JPA, and Hibernate with the save() method of JpaRepository.

JPARepository.save() Method

The save() method in JpaRepository is used to insert or update an entity in the database.

userRepository.save(user);

If the entity does not exist, it will be inserted. If it exists (based on its primary key), it will be updated.

Step By Step Implementation

Step 1: Create a Spring Boot Project

1. Visit Spring Initializr.

2. Select:

3. Add the following dependencies:

Click Generate, extract the project, and open it in your IDE.

**Note: Use JDK 17 or later.

Spring-Initializr

Spring Initializr

Step 2: Configure pom.xml

Ensure your pom.xml contains the required dependencies and uses a supported Java version.

XML `

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
    <relativePath/>
</parent>

<properties>
    <java.version>17</java.version>
</properties>

<dependencies>
    <!-- Spring Boot Starters -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- MySQL Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- Testing -->
    <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>

`

Step 3: Configure Database in application.properties

Set up your database credentials and Hibernate configuration in **src/main/resources/application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/user_db
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

**Note: Replace user_db with your database name and ensure MySQL is running locally.

Step 4: Create Entity Class

Define an entity class to map Java objects to the database table.

**User.java:

Java `

import jakarta.persistence.; import lombok.;

@Entity @Table(name = "users") @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(nullable = false)
private String name;

}

`

Step 5: Create Repository Interface

The repository interface extends JpaRepository to enable CRUD operations.

**UserRepository.java:

Java `

import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;

@Repository public interface UserRepository extends JpaRepository<User, Integer> { }

`

No implementation is required since Spring Data JPA automatically provides one at runtime.

Step 6: Insert Data Using save() Method

Use the CommandLineRunner interface to execute code after the application starts.

Java `

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class SpringBootAppApplication implements CommandLineRunner {

@Autowired
private UserRepository userRepository;

public static void main(String[] args) {
    SpringApplication.run(SpringBootAppApplication.class, args);
}

@Override
public void run(String... args) {
    User firstUser = new User();
    firstUser.setName("Aayush");

    userRepository.save(firstUser);

    System.out.println("User inserted successfully!");
}

}

`

**Step 7: Run the Application

Run the application from your IDE or using the command:

mvn spring-boot:run

The Spring Boot application will start and automatically insert a new record into the users table.

**Output:

Step 8: Verify the Data in MySQL

Execute the following SQL query in MySQL to confirm insertion:

SELECT * FROM users;

**Output: