Use the H2 in-memory database in Spring Boot

In previous articles, we mentioned using an H2 in-memory database in Spring Boot for easy development and testing. In this article, we will provide some more concrete and useful information to help you use the H2 database.

Adding a dependency Configuration

To use H2, we need to add the following configuration:

<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>
Copy the code

Database Configuration

With the above dependencies, by default Spring Boot will automatically create an in-memory H2 database for us to use. Of course, we can also use our own configuration, which we will write to application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Copy the code

By default, the in-memory database is destroyed after the program ends. If we want to store the in-memory database permanently, we need to add the following configuration:

spring.datasource.url=jdbc:h2:file:/data/demo
Copy the code

The file storage address of the database is configured.

Adding initial Data

We can add the data. SQL file to the Resources file to create the required database when the program starts:

DROP TABLE IF EXISTS billionaires;
 
CREATE TABLE billionaires (
  id INT AUTO_INCREMENT  PRIMARY KEY,
  first_name VARCHAR(250) NOT NULL,
  last_name VARCHAR(250) NOT NULL,
  career VARCHAR(250) DEFAULT NULL
);
 
INSERT INTO billionaires (first_name, last_name, career) VALUES
  ('Aliko'.'Dangote'.'Billionaire Industrialist'),
  ('Bill'.'Gates'.'Billionaire Tech Entrepreneur'),
  ('Folrunsho'.'Alakija'.'Billionaire Oil Magnate');
Copy the code

Spring Boot automatically loads the data.sql file at startup. This is a very convenient way for us to test.

Accessing the H2 database

Although it is an in-memory database, we can also access and manage H2 externally. H2 provides an embedded GUI hypervisor. Let’s see how it can be used. You need to add the following permissions:

spring.h2.console.enabled=true
Copy the code

To start the program, we visit http://localhost:8080/h2-console and get the following interface:

Remember to fill in the address and password you configured in the configuration file.

After login, we can see the following admin interface:

We can also add the following configuration to manage the GUI:

spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
Copy the code

Path specifies the path, trace specifies whether to enable trace output, and web-allow-others specifies whether to allow remote login.

Examples of this article can be found at github.com/ddean2009/l…

Please refer to http://www.flydean.com/spring-boot-h2/ for more content