0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【VS Code】Spring Boot × Maven × PostgreSQL × DevTools の開発環境構築手順まとめ

Posted at

はじめに

この記事では、VS Code上でSpring Boot + Maven + PostgreSQLを使った開発環境を構築し、DevToolsによる自動再起動を有効にするまでの手順を解説します。

Spring Boot初心者や、PostgreSQLを使ったAPI開発にチャレンジしたい方にもおすすめの内容です。

前提条件

  • JDK 17 以上(AdoptOpenJDKなど)
  • Maven インストール済み
  • PostgreSQL インストール済み(任意のバージョン)
  • VS Code インストール済み
  • 拡張機能「Extension Pack for Java」インストール済み

1. プロジェクトの作成

方法①: ターミナルからMavenプロジェクト作成

mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=springboot-postgres \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

cd springboot-postgres

方法②: Spring Initializrから作成(おすすめ)

以下のように設定し、プロジェクトをダウンロードしてVS Codeで開きます。

  • Project: Maven
  • Language: Java
  • Dependencies:
    • Spring Web
    • Spring Boot DevTools
    • PostgreSQL Driver

2. pom.xml に依存関係を追加

<dependencies>
  <!-- Spring Boot Web -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!-- Spring Boot DevTools -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
  </dependency>

  <!-- PostgreSQL Driver -->
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.6.0</version>
  </dependency>
</dependencies>

3. application.properties の設定

src/main/resources/application.properties に以下を記述します。

# PostgreSQL接続情報
spring.datasource.url=jdbc:postgresql://localhost:5432/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=org.postgresql.Driver

# JPA設定
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update

# DevTools設定
spring.devtools.restart.enabled=true

4. PostgreSQLの準備

PostgreSQLにて、対象のデータベースとユーザーを作成します。

CREATE DATABASE your_database;
CREATE USER your_username WITH ENCRYPTED PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_username;

5. アプリケーションの実行

以下のコマンドでアプリケーションを起動します。

mvn spring-boot:run

変更を保存するたびに、DevToolsが自動的にアプリケーションを再起動してくれます。

おわりに

以上で、VS Code × Spring Boot × PostgreSQL × DevTools の環境構築は完了です。
DevToolsによって開発効率が大幅に上がりますので、ぜひ導入してみてください!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?