Development

스프링 부팅에서 H2 데이터베이스 구성

sonpro 2023. 3. 1. 15:10
반응형

H2

Configuring H2 Database in Spring Boot

In this article, we will learn how to configure an H2 database in a Spring Boot application. H2 is an open-source, in-memory, Java SQL database. It is very lightweight and its small footprint makes it suitable for use in embedded applications. We will also learn how to access the H2 console to view and manipulate the data stored in the database.

Introduction

H2 is an open-source, in-memory, Java SQL database. It is very lightweight and its small footprint makes it suitable for use in embedded applications. It supports both embedded and server modes. In embedded mode, the database runs in the same JVM as the application. In server mode, the database runs in a separate process, which can be accessed from multiple applications.

H2 is written in Java and is compatible with all major operating systems. It supports a wide range of SQL features, including stored procedures, triggers, and user-defined functions. It also supports the latest JDBC 4.2 API.

Configuring H2 Database in Spring Boot

Configuring an H2 database in a Spring Boot application is straightforward. We need to add the H2 dependency in the pom.xml file.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

We also need to configure the database connection in the application.properties file.

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

The spring.datasource.url property specifies the URL of the database. The spring.datasource.driverClassName property specifies the driver class name of the database. The spring.datasource.username and spring.datasource.password properties specify the username and password of the database.

We can also configure the H2 console in the application.properties file.

spring.h2.console.enabled=true
spring.h2.console.path=/h2

The spring.h2.console.enabled property enables the H2 console. The spring.h2.console.path property specifies the path of the H2 console.

Accessing the H2 Console

Once the H2 database is configured, we can access the H2 console by navigating to the URL http://localhost:8080/h2. This will open the H2 console in the browser.

H2 Console

We can use the H2 console to view and manipulate the data stored in the database.

Summary

In this article, we learned how to configure an H2 database in a Spring Boot application. We also learned how to access the H2 console to view and manipulate the data stored in the database.

반응형