LoginSignup
9
8

More than 1 year has passed since last update.

Spring Boot, Doma2, Gradleの初期設定まとめ

Last updated at Posted at 2020-07-12

始めに

Eclipse で開発する際の、Spring Boot + Doma2 + Gradle の初期設定でいつもハマっているので自分用にまとめる。

環境

  • IDE:Eclipse pleiades-2020-03
  • Spring Boot:2.3.1 RELEASE
  • Doma:2.35.0

設定手順

(省略可)Spring Initializer でアプリケーションのひな型を作成する

Eclipse のプラグイン経由で作成していますが、ブラウザでも curl でもなんでもいいです。

image-01.png

image-02.png

Doma2 への依存関係を追加する

build.gradle
dependencies {
  // ... 省略
  implementation 'org.seasar.doma.boot:doma-spring-boot-starter:1.4.0'
  annotationProcessor 'org.seasar.doma:doma-processor:2.35.0'
}

Eclipse の設定をする

build.gradleの plugins に Eclipse 設定用のプラグインを追加する。

build.gradle
plugins {
  // ...
  id 'com.diffplug.eclipse.apt' version '3.23.0'
}

gradle eclipseを実行し、設定を反映する。

$ ./gradlew eclipse
Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 16s
5 actionable tasks: 5 executed

Eclipse の設定(注釈処理)が完了していることが確認できる。

image-03.png

image-04.png

簡単なAPIを作成する

https://github.com/domaframework/doma-spring-boot/tree/1.4.0の README に沿って、基本的に作成します。

Entityを定義する

Reservation.java
package com.example.demo;

import org.seasar.doma.Entity;

import lombok.Data;

@Data
@Entity
public class Reservation {

    private Integer id;

    private String name;
}

Dao Interface を定義する

ReservationDao.java
package com.example.demo;

import java.util.List;

import org.seasar.doma.Dao;
import org.seasar.doma.Insert;
import org.seasar.doma.Select;
import org.seasar.doma.boot.ConfigAutowireable;
import org.springframework.transaction.annotation.Transactional;

@ConfigAutowireable
@Dao
public interface ReservationDao {

    @Select
    public List<Reservation> selectAll();

    @Insert
    @Transactional
    public int insert(Reservation reservation);
}

ReservationDao#selectAllにカーソルを合わせ、右クリック > Doma > Jump to Sql File を押下すると、空のSQL ファイルが生成されます。(Eclipse に Doma Tools プラグインを追加している場合)

※ DOMA4019 が発生した場合は、(補足)DOMA4019エラーが発生した場合を参照してください。

SQL ファイルにクエリーを書く

selectAll.sql
SELECT
  id,
  name
FROM reservation
ORDER BY name ASC

Service, Controller クラスを定義する

ReservationService.java
package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReservationService {

    @Autowired
    private ReservationDao reservationDao;

    public List<Reservation> selectAll() {
        return reservationDao.selectAll();
    }

    public int insert(Reservation reservation) {
        return reservationDao.insert(reservation);
    }
}

ReservationController.java
package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    @GetMapping(path = "/")
    public List<Reservation> selectAll() {
        return reservationService.selectAll();
    }

    @PostMapping(path = "/")
    public int insert(@RequestBody Reservation reservation) {
        return reservationService.insert(reservation);
    }

}

起動時にReservationテーブルを作成する

HSQLDBという Java 製のインメモリ DB を使います。src/main/resources直下にschema.sqlを配置しておくと、起動時にテーブル作成のスクリプトを流すことができます。

schema.sql
CREATE TABLE reservation (
  id   IDENTITY,
  NAME VARCHAR(50)
);

APIの動作確認

POST(登録)

POST http://localhost:8080 HTTP/1.1
Content-Type: application/json

{
  "id": 1,
  "name": "サンプルA"
}

レスポンス;

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 12 Jul 2020 15:09:30 GMT
Connection: close

1

GET(全件取得)

GET http://localhost:8080 HTTP/1.1

レスポンス;

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 12 Jul 2020 15:10:20 GMT
Connection: close

[
  {
    "id": 1,
    "name": "サンプルA"
  }
]

(補足)DOMA4019 エラーが発生した場合

以下のように、SQL ファイルの絶対パスが期待通りではない、と DOMA4019 エラーが発生した場合について。

[DOMA4019] The file "META-INF/com/example/demo/ReservationDao/selectAll.sql" is not found in the classpath. The absolute path is "C:\Git\springboot-doma2-sample\bin\main\META-INF\com\example\demo\ReservationDao\selectAll.sql".

対象プロジェクト上で、右クリック > プロパティ > Java のビルドパス を修正します。

image-05.png

プロジェクトのデフォルト出力フォルダー ⇒ 特定の出力フォルダー(bin/main)に修正。

image-06-01.png

終わりに

作成したアプリケーションは、リポジトリに格納しました。build.gradleの全量を参照したい場合などに見てください。

参考

9
8
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
9
8