application.properties
for MySQL JDBC configuration:# JDBC Configuration
spring.datasource.url=jdbc:mysql://your-database-host:your-port/your-database-name?useSSL=false&serverTimezone=UTC
spring.datasource.username=your-username
spring.datasource.password=your-password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.maximum-pool-size=10 # Optional, to set the connection pool size
spring.datasource.hikari.minimum-idle=5 # Optional, to set the minimum idle connections
spring.datasource.hikari.idle-timeout=30000 # Optional, set timeout for idle connections in milliseconds
# JPA / Hibernate Configuration (optional, if you are using JPA)
spring.jpa.hibernate.ddl-auto=update # Optional, to define Hibernate behavior
spring.jpa.show-sql=true # Optional, to log SQL queries
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect # Dialect for MySQL DB
# Additional optional configuration
spring.datasource.initialization-mode=always # Optional, to always initialize the database schema at startup
Explanation of key properties:
spring.datasource.url
: The JDBC URL for the MySQL database. Replace your-database-host
, your-port
, and your-database-name
with the actual database details.
Example:
jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
for a local MySQL instance on port 3306 with database mydb
.The parameters useSSL=false
and serverTimezone=UTC
are used to avoid SSL-related warnings and set the timezone for MySQL connections.
spring.datasource.username
: The username to connect to the MySQL database.
spring.datasource.password
: The password for the provided username.
spring.datasource.driver-class-name
: The MySQL JDBC driver class: com.mysql.cj.jdbc.Driver
.
spring.datasource.hikari.maximum-pool-size
: The maximum number of connections in the connection pool (you can configure this as needed).
spring.datasource.hikari.minimum-idle
: The minimum number of idle connections in the pool (configurable).
spring.jpa.hibernate.ddl-auto
: Configures how Hibernate handles database schema updates. Options include none
, update
, create
, or create-drop
.
spring.jpa.properties.hibernate.dialect
: Defines the Hibernate dialect for MySQL. In this case, MySQL5InnoDBDialect
is used.
spring.jpa.show-sql
: Enables the logging of SQL queries.
spring.datasource.initialization-mode
: Configures whether the schema should be initialized when the application starts.
If you haven't added the MySQL JDBC driver to your project yet, include the following dependency in your pom.xml
(Maven) file:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version> <!-- Use the latest version -->
</dependency>
If you're using Gradle, add this to your build.gradle
file:
implementation 'mysql:mysql-connector-java:8.0.28' // Use the latest version
useSSL=false
option disables SSL encryption for the connection. You can set this to true
if you're using SSL in a production environment.