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

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://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>2.7.0</version>
        <relativePath/>
    </parent>
    <groupId>com.gfg</groupId>
    <artifactId>springboot_mysql_project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_mysql_project</name>
    <description>Demo project for Spring Boot with MySQL</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <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 dependency -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</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>

Step 2: Configure MySQL Connection

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

Java
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/geeksforgeeks?serverTimezone=UTC&useSSL=false&autoReconnect=true
spring.datasource.username=**** 
spring.datasource.password=****

Step 3: Create the Main Class

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

Java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Step 4: Create Entity Class

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

Book.java

Java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

// This tells Hibernate to make
// a table out of this class
@Entity 
public class Book {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String bookName;

    private String isbnNumber;

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getIsbnNumber() {
        return isbnNumber;
    }

    public void setIsbnNumber(String isbnNumber) {
        this.isbnNumber = isbnNumber;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    
}

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

Java
package com.gfg;

import org.springframework.data.repository.CrudRepository;

// This will be AUTO IMPLEMENTED by Spring
// into a Bean called Book
// CRUD refers Create, Read, Update, Delete
public interface BookRepository extends CrudRepository<Book, Integer> {

}

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

Java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

// This means that this 
// class is a Controller
@Controller    

// This means URL's start with /geek (after Application path)
@RequestMapping(path="/geek") 
public class BookController {
  
    // This means to get the bean called geekuserRepository
    // Which is auto-generated by Spring, we will use it
      // to handle the data
    @Autowired 
    private BookRepository bookRepository;

    // Map ONLY POST Requests
    @PostMapping(path="/addbook") 
    public @ResponseBody String addBooks (@RequestParam String bookName
            , @RequestParam String isbnNumber) {
      
        // @ResponseBody means the returned String
          // is the response, not a view name
        // @RequestParam means it is a parameter
          // from the GET or POST request
      
        Book book = new Book();
        book.setBookName(bookName);
        book.setIsbnNumber(isbnNumber);
        bookRepository.save(book);
        return "Details got Saved";
    }

    @GetMapping(path="/books")
    public @ResponseBody Iterable<Book> getAllUsers() {
        // This returns a JSON or XML with the Book
        return bookRepository.findAll();
    }
}

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 :

Console Output:

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.

We can check the same by executing the below URL

http://localhost:8080/geek/books

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

Let us check the same in MySQL as well

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