How to Connect Your Spring Boot App with MySQL

박진석·2025λ…„ 2μ›” 9일
0

FindMyBMW

λͺ©λ‘ 보기
3/10
post-thumbnail

How to Connect Your Spring Boot App with MySQL πŸš€

πŸ‘‹ We're going to walk through connecting your Spring Boot application to a MySQL database.

Prerequisites πŸ“

Before we start, make sure you have:

  • MySQL installed on your computer

Step 1: Add the Dependencies πŸ“¦

First, let's add the necessary dependencies to your build.gradle file. These are like telling your application what tools it needs to work with MySQL.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'mysql:mysql-connector-java'
    // Your other dependencies...
}

Step 2: Configure Your Database Connection πŸ”Œ

Now, let's tell your application how to find and connect to your MySQL database. Create or open src/main/resources/application.properties and add these lines:

# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password

# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

Remember to replace:

  • your_database_name with your actual database name
  • your_username with your MySQL username
  • your_password with your MySQL password

Step 3: Create an Entity Class πŸ—οΈ

Entities are like blueprints for your database tables.

@Entity
@Table(name = "your_table_name")
@Data  // If you're using Lombok
public class YourEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String name;
    // Add other fields as needed
}

Step 4: Create a Repository Interface πŸ—ƒοΈ

Repositories help you interact with your database without writing SQL queries:

@Repository
public interface YourRepository extends JpaRepository<YourEntity, Long> {
    // Spring Data JPA will implement this interface for you!
}

Step 5: Test Your Connection πŸ§ͺ

Let's create a simple test to make sure everything works:

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class YourRepositoryTest {
    @Autowired
    private YourRepository repository;

    @Test
    void testDatabaseConnection() {
        // Your test code here
    }
}

Common Issues and Solutions πŸ”§

  1. Connection Refused

    • Make sure MySQL is running
    • Check if the port (default 3306) is correct
    • Verify your database name exists
  2. Authentication Failed

    • Double-check your username and password
    • Ensure the user has proper permissions
  3. Table Doesn't Exist

    • Check if spring.jpa.hibernate.ddl-auto=update is set
    • Verify table names in your @Table annotations

Tips πŸ’‘

  • Always use environment variables or a secure configuration for sensitive data like passwords
  • Enable SQL logging during development to see what's happening under the hood
  • Keep your database credentials safe and never commit them to version control
  • Use meaningful names for your entities and repositories

0개의 λŒ“κΈ€