Skip to main content

Databases: H2 and MySQL

Spring Boot heavily abstracts mapping Database settings through standard auto-configuration boundaries securely.

Spring Boot H2 Database

H2 is a specific relational database that writes strictly entirely into system RAM rather than saving persistently onto a physical disk. It is the standard choice for executing rapid integration tests natively.

Configuration

If you add the h2 database dependency smoothly into your pom.xml, Spring Boot boots up an H2 instance implicitly without writing any connection string in application.properties.

It also provides an automatic web console securely mapped to http://localhost:8080/h2-console to physically view the memory tables in a browser.

Connecting to MySQL

Unlike H2, a persistent database like MySQL requires exact target credentials explicitly.

In your src/main/resources/application.properties file, you assign the structural JDBC coordinates securely. Spring Boot reads these values natively and constructs the DataSource bean correctly on boot.

# System application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=root
spring.datasource.password=securepass123

# Determines if Hibernate should overwrite your SQL schema structure automatically
spring.jpa.hibernate.ddl-auto=update