0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SpringBoot から Supabase DB接続 の簡易メモ

Last updated at Posted at 2025-04-24

DB を無料で使えるサービス Supabase を使って SpringBoot から接続したときの簡易メモ
Supabase は認証とかもあって使い道がいろいろとありそう
一旦は PostgreSQL を無料で使えるくらいでやってます

大雑把な流れ

  • Supabase にプロジェクト作成・テーブル作成
  • SpringBoot のプロジェクト作成・DB接続情報定義・モジュール作成

実行環境

  • Windows11
  • Pleiades 2024 Full Edition
  • Maven v3.9.6
  • SpringFramework Boot v3.4.4
  • Java v21
  • spring-boot-starter-data-jdbc
  • spring-boot-starter-data-jpa
  • PostgreSQL Driver

Supabase にプロジェクト作成・テーブル作成

基本は流れに身を任せて進めれば作れると思います
参考程度にイメージはって手順のせてます
後続のデータ取得でつくる Entity, Repository はここで作ってるテーブルの設定でつくります

Supabasee プロジェクト作成

  1. Supabasee のサイトにアクセス
    https://supabase.com/
  2. Start your project でアカウント作成に進む
    image.png
  3. アカウントをつくる(イメージは Github でつくるとき)
    image.png
  4. 各種の設定は初期設定でOK (料金 free じゃなかったら free にした方が安全)
    image.png
  5. プロジェクト作成(とりあえず blank)
    image.png
  6. プロジェクト名、リージョン(日本にする)、パスワード入力(覚えておく)
    image.png

Supabasee テーブル作成

  1. Table Editor 選んで Create a new table を押す
    image.png
  2. 以下の設定でテーブル作成
    • Name : wktable
    • Enable Row Level Security(RLS) :チェックあり
    • Columns
      • id : 最初のまんま
      • created_at : 最初のまんま
      • message:Typeをtext
        image.png
  3. テーブルのRLSポリシーを追加 (Add RLS policy)
    ここでは全部、許可の設定(要件に合わせてやった方が良いです)
    • Policy Name : なんでもOK
    • Policy Behavior : Permissive
    • Policy Command : とりあえず ALL
    • Target Roles : とりあえず public
    • using, with check : true
      image.png

SpringBoot のプロジェクト作成・DB接続定義・モジュール作成

最低限の設定だけつけてDBの情報をコンソールログにとりあえずだすまでやってみる

SpringBoot プロジェクト作成

  1. SpringBoot の新規プロジェクトを作成する
    以下のところが気にするところ、それ以外はデフォルトでOK
    • タイプ:Maven
    • Javaバージョン:21
    • 依存関係:Spring Data JDBC, Spring Data JPA, PostgreSQL Driver をチェック(他はデフォルトのチェック残す)

SpringBoot DB接続定義

  1. application.properties に以下を追加

    • url : これは supabase より取得する URI (取得方法ちょっとしたを参照)
    • pool : とりあえず最小の設定(デフォルトだと何回か再起動でエラーになる)
    application.properties
    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.url=jdbc:postgresql://xxxxxxx.suapbase.com:6543/postgres?user=xxxxxxx&password=[YOUR-PASSWORD]
    spring.datasource.hikari.maximum-pool-size=1
    spring.datasource.hikari.minimum-idle=1
    spring.datasource.hikari.connection-timeout=30000
    spring.datasource.hikari.idle-timeout=300000
    spring.datasource.hikari.max-lifetime=1800000
    spring.datasource.hikari.leak-detection-threshold=5000
    spring.datasource.hikari.pool-name=SupabaseHikariPool
    spring.datasource.hikari.connection-test-query=SELECT 1
    
    spring.jpa.hibernate.ddl-auto=none
    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect    
    
    • supabase で URI を取得( Connect → Type を JDBC → Transaction pooler)
      image.png

SpringBoot モジュール作成

  1. Entity
    Wktable.java
    package com.example.demo;
    
    import java.util.Date;
    
    import org.hibernate.annotations.CreationTimestamp;
    
    import jakarta.persistence.Entity;
    import jakarta.persistence.GeneratedValue;
    import jakarta.persistence.GenerationType;
    import jakarta.persistence.Id;
    import lombok.Data;
    
    @Data
    @Entity
    public class Wktable {
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Long id;
    	@CreationTimestamp
    	private Date createdAt;
    	private String message;
    
    }
    
  2. Repository
    WktableRepository.java
    package com.example.demo;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface ChatRepository extends JpaRepository<Chat, Long> {
    }
    
  3. Controller
    GetDbController.java
    package com.example.demo;
    
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class GetDbController {
    
    	@Autowired
    	WktableRepository wktableRepository;
    
    	@GetMapping("/getdb")
    	public List<Wktable> getdb() {
    		List<Wktable> wktables = wktableRepository.findAll();
    		wktables.forEach(wktable -> {
    			System.out.println(wktable.getId());
    			System.out.println(wktable.getCreatedAt());
    			System.out.println(wktable.getMessage());
    		});
    		return wktables;
    	}
    }
    
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?