Open In App

Spring Boot Integration With MySQL as a Maven Project

Last Updated : 09 Oct, 2022
Like Article

Spring Boot is trending and it is an extension of the spring framework but it reduces the huge configuration settings that need to be set in a spring framework. In terms of dependencies, it reduces a lot and minimized the dependency add-ons. It extends maximum support to all RDBMS databases like MySQL and NoSQL databases like MongoDB. In this article let us see a sample project connecting Spring Boot and MySQL.


Project Structure:

Project Structure


This is a maven project



<?xml version="1.0" encoding="UTF-8"?>
    <description>Demo project for Spring Boot with MySQL</description>
        <!-- MySQL dependency -->

The important file that helps to tell about MySQL connectivity information

spring.datasource.username=****#Specify the proper user name
spring.datasource.password=****#Specify the proper password then only the application can be connected with MySQL

Spring boot can be run as Java application, which means there should be the main class


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class SampleAccessingOfMysqlApplication {
    public static void main(String[] args) {, args);

Let us start with the bean class


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
public class Book {
    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) { = id;


package com.gfg;
// 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> {


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
// This means URL's start with /geek (after Application path)
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
    private BookRepository bookRepository;
    // Map ONLY POST Requests
    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();
        return "Details got Saved";
    public @ResponseBody Iterable<Book> getAllUsers() {
        // This returns a JSON or XML with the Book
        return bookRepository.findAll();

The application can be run as follows :


Console Output:


Now we can run and see the following. As we are entering the book details via POST, let us use the Postman client to execute the same

--Note : In the controller, it has been given that all urls should have geek as pattern
and after that 'addbook' is the mapping that helps to add the books to the table named 'book'.
As in,
is available, if there is no table named 'book' is present, it is automatically created


We can check the same by executing the below URL



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.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads