Spring JPAでPostgreSQLに接続する方法

Spring JPAでPostgreSQLに接続する方法

Spring JPAでPostgreSQLに接続する方法です。

STSのスタータープロジェクトで以下を追加しておきます。

Spring JPAでPostgreSQLに接続する方法

application.yamlでDataSourceを指定します。ここでは、DB=postgres,username=postgres,password=postgresです。PostgreSQLはデフォルトポートは5432です。

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: postgres

これでSpring Applicationを起動します。アプリケーション起動時のコンソールに以下表示されればOKです。

Initialized JPA EntityManagerFactory for persistence unit 'default'
Spring Data repositories initialized!

@Repositoryアノテーションをついたリポジトリインタフェース、テーブルに対してエンティティクラスを用意すればテーブル操作が可能です。

GitHub - takahashi-h5/jpa-postgresql
Contribute to takahashi-h5/jpa-postgresql development by creating an account on GitHub.

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました