How do you override the default database in a Spring Boot application?
Table of Contents
- Introduction
- Steps to Override the Default Database in Spring Boot
- Conclusion
Introduction
By default, Spring Boot configures an in-memory H2 database for you when you do not specify a data source configuration. This default setup is ideal for development and testing but is not suitable for production applications where a persistent, external database is required. To connect to a different database, such as MySQL, PostgreSQL, or Oracle, you can easily override the default database configuration in Spring Boot.
This guide will walk you through the steps to override the default H2 database with a custom database configuration in your Spring Boot application.
Steps to Override the Default Database in Spring Boot
1. Add Database Dependencies
The first step is to add the necessary dependencies for the database you want to use. For example, to use MySQL or PostgreSQL, you need to include their respective dependencies in your pom.xml
(Maven) or build.gradle
(Gradle) file.
For Maven:
- MySQL dependency:
- PostgreSQL dependency:
For Gradle:
- MySQL dependency:
- PostgreSQL dependency:
2. Configure the Database Connection
After adding the required dependencies, the next step is to configure the connection details for the new database in your application.properties
or application.yml
file.
For MySQL:
- spring.datasource.url: The URL for the MySQL database (replace
localhost:3306/mydatabase
with your actual database host and name). - spring.datasource.username: The username to access the database.
- spring.datasource.password: The password for the database.
- spring.jpa.database-platform: The Hibernate dialect for MySQL (
MySQL8Dialect
). - spring.jpa.hibernate.ddl-auto: Defines how Hibernate handles schema updates (e.g.,
update
,create
,none
). - spring.jpa.show-sql: Enables the logging of SQL statements.
For PostgreSQL:
- spring.datasource.url: The URL for the PostgreSQL database (replace
localhost:5432/mydatabase
with your actual database host and name). - spring.datasource.username: The username to access the database.
- spring.datasource.password: The password for the database.
- spring.jpa.database-platform: The Hibernate dialect for PostgreSQL (
PostgreSQLDialect
).
If you are using other databases like Oracle, SQL Server, or SQLite, you will need to adjust the JDBC URL, username, password, and dialect accordingly.
3. Optional: Configure JPA Properties (Advanced)
Spring Boot automatically configures JPA for you, but if you need advanced settings (such as caching, entity scanning, or custom Hibernate configurations), you can add additional properties.
For example, to enable Hibernate’s second-level cache, you can add:
4. Create JPA Entities and Repositories
If you haven’t already done so, you will need to create JPA entities and repositories to interact with the database. For example:
Employee
Entity:
EmployeeRepository
Interface:
5. Test the Configuration
After configuring your database connection and adding the necessary entities, you can run your Spring Boot application. Spring Boot will now connect to the specified database instead of the default H2 database.
You can also use tools like Postman or curl to test the API endpoints (if you’ve created controllers and services for your entities) to ensure that the application is interacting with the database correctly.
6. Changing Database Profiles (Optional)
Spring Boot allows you to have different configurations for different environments (e.g., development, testing, production). You can specify a different profile for each environment using the application.properties
or application.yml
files.
For example, you can create an application-dev.properties
for the development environment:
Then, when running the application, you can specify the active profile:
Conclusion
Overriding the default H2 database in Spring Boot is a simple process, mainly involving configuring the data source and providing the appropriate database dependencies. You can connect to various databases like MySQL, PostgreSQL, or others by adjusting your application.properties
or application.yml
file with the correct connection details.
By overriding the default database configuration, you can easily set up a persistent external database for your Spring Boot application and ensure it is ready for production use.