Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 25

    MySQL Integration

    TechLearningPRO Orders often lands on MySQL because an existing estate already runs it.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    TechLearningPRO Orders often lands on MySQL because an existing estate already runs it. Spring Boot 3 still prefers PostgreSQL for greenfield work, but MySQL integration is the same pattern: driver on the classpath, JDBC URL, credentials from the environment, Hibernate dialect, HikariCP pool.

    Add mysql-connector-j (runtime). Point spring.datasource.url at jdbc:mysql://… with serverTimezone=UTC and prefer TLS in real deployments. Boot auto-configures a DataSource and, with spring-boot-starter-data-jpa, a EntityManagerFactory.

    Dialect and charset matter. Use org.hibernate.dialect.MySQLDialect (or the version-specific dialect Boot selects) and store Unicode as utf8mb4. Money stays integer cents — never floating point — same as Postgres lessons.

    This lesson contrasts drivers and dialects with PostgreSQL. The repository and entity APIs stay identical; only the wire protocol and a few SQL quirks change.

    Purpose of this lesson

    Wire Spring Boot 3 Orders to MySQL with the official connector, HikariCP, Jakarta persistence, and production-safe ddl-auto/migration habits.

    Understanding the topic

    MySQL is a JDBC target — Boot does the rest when the classpath is right.

    • Driver: com.mysql:mysql-connector-j at runtime scope.
    • URL: jdbc:mysql://host:3306/orders plus timezone and SSL flags.
    • Dialect: Hibernate MySQL dialect — distinct from PostgreSQLDialect.
    • Pool: HikariCP is Boot's default; size it for checkout concurrency.
    • Schema: ddl-auto: validate in prod; Flyway/Liquibase owns DDL.

    Step-by-step explanation

    1. Add mysql-connector-j and spring-boot-starter-data-jpa to the Orders POM.
    2. Set datasource URL, username, and password from env placeholders — not committed secrets.
    3. Set spring.jpa.hibernate.ddl-auto=validate and confirm the MySQL dialect.
    4. Start the app against a local MySQL orders database; verify Hikari acquires a connection.

    Informative example

    MySQL driver + datasource for Orders. Contrast: Postgres uses org.postgresql:postgresql and jdbc:postgresql://….

    yaml
    // pom.xml (excerpt)
    <dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
    </dependency>
    # application.yml
    spring:
    datasource:
    url: jdbc:mysql://localhost:3306/orders?serverTimezone=UTC&characterEncoding=utf8
    username: ${DB_USER}
    password: ${DB_PASS}
    hikari:
    maximum-pool-size: 20
    minimum-idle: 5
    jpa:
    hibernate:
    ddl-auto: validate
    open-in-view: false
    properties:
    hibernate.dialect: org.hibernate.dialect.MySQLDialect

    Output

    HikariPool-1 - Start completed.
    HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect

    Real-world use

    A legacy TechLearningPRO billing cluster already runs MySQL 8. Orders joins that estate: same entity model as the Postgres track, different driver URL and dialect. Checkout still goes Controller → Service → Repository; only ops changes the connection string.

    Security implications

    Datasource credentials are production secrets.

    • Inject DB_USER/DB_PASS from a secret store — never commit them.
    • Grant the app user DML only; migrations run with a separate migrator role.
    • Require encrypted connections to cloud MySQL endpoints.

    Best practices

    • Pin pool size; do not leave Hikari at defaults under load tests.
    • Prefer TLS (useSSL=true / modern equivalent) toward managed MySQL.
    • Keep Flyway migrations dialect-aware when you must support both MySQL and Postgres.

    Common mistakes

    • Forgetting serverTimezone=UTC and getting shifted created_at values.
    • Using utf8 instead of utf8mb4 and truncating emoji or rare Unicode in notes fields.
    • Copy-pasting a Postgres URL into a MySQL profile and debugging 'protocol' errors for an hour.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhich artifact connects Boot 3 to MySQL?+

    Answer

    com.mysql:mysql-connector-j (runtime). Older mysql-connector-java names are legacy.
    2IntermediateQuestionWhat differs between MySQL and PostgreSQL integration in Spring Boot?+

    Answer

    Primarily the JDBC driver artifact, JDBC URL scheme, and Hibernate dialect — JPA repositories and Jakarta entity annotations stay the same.
    3IntermediateQuestionWhy use ddl-auto validate with MySQL in production?+

    Answer

    Schema ownership belongs to versioned migrations. Validate fails fast on drift instead of silently mutating tables.

    Hands-on exercise

    Lab. Create a local MySQL schema orders. Wire the YAML above with env vars. Boot the app and confirm the dialect log line. Then swap the driver/URL to Postgres and note which files change — entities should not.

    Summary

    MySQL integration is classpath and configuration. Next, PostgreSQL — the preferred Orders database on this track — with the same JPA surface.

    Ready to mark this lesson complete?Track your journey across the entire course.