VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-boot-integration-with-mysql-as-a-maven-project/

⇱ Spring Boot Integration With MySQL as a Maven Project - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot Integration With MySQL as a Maven Project

Last Updated : 4 Apr, 2026

Spring Boot simplifies backend development by providing auto-configuration and embedded server support. Integrating it with MySQL allows developers to build robust, database-driven applications efficiently. Using Maven further helps manage dependencies and project structure easily.

  • Simplifies database integration using Spring Boot
  • Uses Maven for dependency and build management
  • Enables rapid development of data-driven applications

Steps to Implement of Spring Boot Integration With MySQL

Step 1: Create a Spring Boot Project

Generate a project using Spring Initializr and fill in the details as per the requirements. For this application:

  • Project: Maven
  • Language: Java
  • Spring Boot Version: Latest stable version
  • Group: com.example
  • Artifact: springboot-mysql-project
  • Packaging: Jar
  • Java Version: 17 (or higher)
  • Dependencies:Spring Web, Spring Data JPA

Click on Generate which will download the starter project.

👁 out
spring-initializr

Project Structure:

Once the project open in your ide (Eclipse) the directory structure looks like as below

👁 Project Structure
Directory -structure

pom.xml

Step 2: Configure MySQL Connection

Add the MySQL database configuration in application.properties to connect Spring Boot with MySQL.

Step 3: Create the Main Class

Create the main class (SampleAccessingOfMysqlApplication.java) to run the Spring Boot application as a Java application.

Step 4: Create Entity Class

Create a bean/entity class (Book.java) that represents the book table in the database.

Book.java

Explanation: This class represents a JPA entity that maps to a database table for storing book details with an auto-generated ID.

Step 5: Create Repository Interface

Create BookRepository.java to handle database operations using Spring Data JPA.

BookRepository.java

Step 6: Create Controller Class

Create BookController.java and define API endpoints such as /geek/addbook to add books and /geek/books to retrieve all books.

BookController.java

Explanation: This Spring boot controller handles adding and retrieving book data by interacting with the repository and exposing endpoints for saving and fetching books

Step 7: Run the Application

Run the main class and wait for the server to start. The application will run on http://localhost:8080

The application can be run as follows :

👁 Image

Console Output:

👁 Image
Output Log

Explanation: This log indicates that the Spring Boot application has successfully started, initialized Hibernate/JPA, and is running on Tomcat at port 8080.

Step 8: Test the API

Use Postman to send a POST request to to add book details.

👁 Image

We can check the same by executing the below URL

http://localhost:8080/geek/books
👁 Image

Similarly, we can add the books we want and can add

👁 Image

Let us check the same in MySQL as well

👁 Image

So it is easier to connect MySQL and Spring Boot. Efficiently we can integrate spring boot and MySQL as in the above sample project.

Comment
Article Tags: