MySQL with Spring Boot

All Java Topics
Last updated: May 25, 2026
Author: ManaCoding Team

MySQL with Spring Boot allows you to connect your application to a relational database using Spring Data JPA and perform CRUD operations easily.

📝 Syntax
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=root
spring.datasource.password=your_password
💻 Example Program
// 1. application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

// 2. Entity Class
import jakarta.persistence.*;

@Entity
class User {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String name;
}

// 3. Repository
import org.springframework.data.jpa.repository.JpaRepository;

interface UserRepository extends JpaRepository<User, Long> {}

// 4. Service
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
class UserService {

  @Autowired
  private UserRepository repo;

  public List<User> getAllUsers() {
    return repo.findAll();
  }

  public User saveUser(User user) {
    return repo.save(user);
  }
}

// 5. Controller
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
class UserController {

  @Autowired
  private UserService service;

  @GetMapping
  public List<User> getUsers() {
    return service.getAllUsers();
  }

  @PostMapping
  public User createUser(@RequestBody User user) {
    return service.saveUser(user);
  }
}

// Output:
// REST API connected with MySQL successfully
💡 What is MySQL with Spring Boot?
  • 1 Integration of Spring Boot with MySQL database.
  • 2 Uses JDBC driver and JPA.
  • 3 Supports CRUD operations.
  • 4 Common in enterprise apps.
💡 Required Dependencies
  • 1 Spring Data JPA
  • 2 MySQL Driver
  • 3 Spring Web
  • 4 Spring Boot Starter
💡 Configuration Steps
  • 1 Add MySQL dependency.
  • 2 Configure application.properties.
  • 3 Create Entity class.
  • 4 Create Repository interface.
  • 5 Build Service and Controller.
💡 Why Use MySQL with Spring Boot?
  • 1 Reliable relational database.
  • 2 Easy integration with Spring.
  • 3 Scalable backend systems.
  • 4 Widely used in industry.
Quick Summary
  • Spring Boot easily connects with MySQL.
  • Uses Spring Data JPA for CRUD operations.
  • Configured via application.properties.
  • Common in enterprise backend systems.
FAQs
How do you connect MySQL with Spring Boot?
By adding MySQL dependency and configuring application.properties.
Which driver is used for MySQL?
MySQL Connector/J.
What is ddl-auto in Spring Boot?
It controls database schema generation.
Which layer interacts with database?
Repository layer.
Which ORM is used with MySQL in Spring Boot?
Hibernate via Spring Data JPA.
🎯 Interview Questions
Q1. How do you connect MySQL with Spring Boot?
Answer: By adding MySQL dependency and configuring application.properties.
Q2. Which driver is used for MySQL?
Answer: MySQL Connector/J.
Q3. What is ddl-auto in Spring Boot?
Answer: It controls database schema generation.
Q4. Which layer interacts with database?
Answer: Repository layer.
Q5. Which ORM is used with MySQL in Spring Boot?
Answer: Hibernate via Spring Data JPA.
Quiz

Which property is used to configure database URL?