Introduction JDBC and JPA starters have built in support for h2 database. But we can make the most out of JPA starter with features like schema generation.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
Here, the starter dependency will take care of all the configuration. At this point, you can simply start the application and spring boot will configure the database automatically.
H2 Database Configuration
By default, Spring Boot creates a h2 database user called SA with no password . But, we can change these values using the application.properties.
spring.datasource.url=jdbc:h2:mem:local spring.datasource.username=someuser spring.datasource.password=somepassword
As you see, the datasource URL is pointing to an in-memory version of h2 database called local. However, you can use a file based h2 database using the following settings.
spring.datasource.url=jdbc:h2:c:/test/local
After this change, you can see that a new DB file at the configured location.
The primary use of H2 database is for testing and prototyping. This means the application has to load data into the database every time when the application starts. for this, spring boot offers automatic schema creation for embedded database types. For instance, Spring boot will automatically create database schema for hibernate entities by default.
You can also load data related to these entities using src/main/resources/data.sql file.
Also, you can specify a different data file using the “spring.datasource.data” property.
spring.datasource.data=c:/test/initial-data.sql
For Example, You can define a entity like below and spring boot will take care of schema generation.
@Entity public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String firstName; private String lastName; }
As you see in the below screenshot, Spring boot autoconfiguration created the USER_ENTITY table and also loaded three records.
Important thing to note here is that, the automatic schema generation is controlled by spring.datasource.initialization-mode property. By default, it is set to embedded. So spring boot will always initialize JPA schema. If you want to disable it, then you need to set it to never.
Also, the schema generation is part of JPA starter. If you are are using spring-boot-starter-jdbc, then you should create a schema.sql to build your database tables.
H2 Console
H2 Console is a web UI for managing the in-memory database. To enabled this UI, first you need to add the following property to your application.properties.
spring.h2.console.enabled=true
Once done, you can open for accessing the UI. To login, you need a db URL, username and password. Here, We know that the username is SA and password is empty string by default. But the DB name itself is an auto generated one. For this reason, you should always provide the values mentioned in the H2 configuration section.
You can configure this screen to some extent. For example, you can change the h2-console URL using spring.h2.console.path configuration. You can even setup an admin password using spring.h2.console.settings.web-admin-password configuration.
Conclusion
Hope you learned about h2 database and how to use and configure them along with Spring Boot. If you want to, You can checkout the examples in this github repository.
You can also learn more about other embedded databases like Apache derby with Spring Boot.
Read further about,