Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
The H2 database is an in-memory and embedded database, which is often used in development and testing environments because of its lightweight nature, easy setup, and fast performance. It's also useful for proof-of-concept projects or when you need a simple persistent storage without setting up a standalone database.
When integrated with Spring Boot, the H2 database provides automatic configuration and an easy-to-use console to view and query the database content. Here's a step-by-step guide to integrating and using the H2 database with Spring Boot:
First, add the required dependencies in your pom.xml
(if you are using Maven):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
Modify your application.properties
or application.yml
file to configure H2 and enable the H2 console:
# Enabling H2 Console spring.h2.console.enabled=true # Datasource Configuration spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
You can now define JPA entities and repositories just like you would with any other database.
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; // getters, setters, constructors... } @Repository public interface UserRepository extends JpaRepository<User, Long> { }
Once your Spring Boot application is running, you can access the H2 console via:
http://localhost:8080/h2-console
Ensure that the JDBC URL in the console matches your configuration (jdbc:h2:mem:testdb
in this case).
If you want to populate your H2 database with some data at startup, you can create a data.sql
file in the src/main/resources
directory. Spring Boot will automatically execute this SQL file after the entities have been created.
For instance, if you want to insert a user at startup:
INSERT INTO user (name) VALUES ('John Doe');
By default, H2 operates in an in-memory mode, meaning data is lost once the application stops. If you want persistent storage, switch to file-based storage by modifying the datasource URL:
spring.datasource.url=jdbc:h2:file:/path/to/your/database/file
While H2 is excellent for development and testing:
By following these steps, you can seamlessly integrate the H2 database with your Spring Boot application, facilitating rapid development and testing.
Configuring H2 Database in Spring Boot:
pom.xml
or build.gradle
.application.properties
or application.yml
.Example (application.properties
):
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.h2.console.enabled=true
In-memory database usage in Spring Boot with H2:
jdbc:h2:mem:
URL for an in-memory H2 database.Example (application.properties
):
spring.datasource.url=jdbc:h2:mem:testdb
Creating tables and schemas in H2 Database with Spring Boot:
Example (User
entity):
import javax.persistence.Entity; import javax.persistence.Id; @Entity public class User { @Id private Long id; // Other fields, getters, and setters }
Example (UserRepository
interface):
import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { // Custom queries if needed }
H2 Console integration in Spring Boot applications:
/h2-console
during development.Example (application.properties
):
spring.h2.console.enabled=true
H2 Database persistence with Spring Data JPA in Spring Boot:
JpaRepository
for basic CRUD operations.Example (UserService
class):
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserRepository userRepository; // Service methods using userRepository }
Data initialization and seed data with H2 in Spring Boot:
data.sql
or schema.sql
in the src/main/resources
folder for initialization.Example (data.sql
):
INSERT INTO user (id, username, email) VALUES (1, 'john_doe', 'john@example.com');
Testing with H2 Database in Spring Boot:
application-test.properties
or application-test.yml
with H2 properties.Example (application-test.properties
):
spring.datasource.url=jdbc:h2:mem:testdb
Example (Test class):
@RunWith(SpringRunner.class) @SpringBootTest public class UserServiceTest { @Autowired private UserService userService; // Write your test cases using H2 as the test database }