MySQL Integration
TechLearningPRO Orders often lands on MySQL because an existing estate already runs it.
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-jat runtime scope. - URL:
jdbc:mysql://host:3306/ordersplus 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: validatein prod; Flyway/Liquibase owns DDL.
Step-by-step explanation
- Add
mysql-connector-jandspring-boot-starter-data-jpato the Orders POM. - Set datasource URL, username, and password from env placeholders — not committed secrets.
- Set
spring.jpa.hibernate.ddl-auto=validateand confirm the MySQL dialect. - Start the app against a local MySQL
ordersdatabase; verify Hikari acquires a connection.
Informative example
MySQL driver + datasource for Orders. Contrast: Postgres uses org.postgresql:postgresql and jdbc:postgresql://….
// pom.xml (excerpt)<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency># application.ymlspring:datasource:url: jdbc:mysql://localhost:3306/orders?serverTimezone=UTC&characterEncoding=utf8username: ${DB_USER}password: ${DB_PASS}hikari:maximum-pool-size: 20minimum-idle: 5jpa:hibernate:ddl-auto: validateopen-in-view: falseproperties: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_PASSfrom 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=UTCand getting shiftedcreated_atvalues. - Using
utf8instead ofutf8mb4and 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.
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
3IntermediateQuestionWhy use ddl-auto validate with MySQL in production?+
Answer
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.