LoginSignup
14

More than 5 years have passed since last update.

Spring boot + GradleでDoma2を使う

Posted at

この記事でわかること

  • Spring bootでGradleを利用してDomaを使うための方法

この記事に書いていないこと

  • Domaの機能に対する説明

前提条件

この記事で使用しているもろもろのバージョン。

  • JDK 8
  • Spring boot 1.3.5.RELEASE
  • Gradle 2.13

依存関係の追加

build.gradleのdependenciesに必要な依存関係を追加します。

compile('org.seasar.doma.boot:doma-spring-boot-starter:1.0.2')
compile('mysql:mysql-connector-java:5.1.38')

コネクターがMySQLになっていますが、使用するDBに併せて適宜変更してください。

また、Domaが扱うSQLファイルをコンパイルより前に出力先ディレクトリにコピーする必要があるため、build.gradleに以下の記述を追加します。

[参考]SpringBoot+Doma2+Gradleを試してみた

bulid.gradle
processResources.destinationDir = compileJava.destinationDir
compileJava.dependsOn processResources

結果としてbuild.gradleは以下のようになります。

build.gradle
// ・・・(省略)
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

    processResources.destinationDir = compileJava.destinationDir
    compileJava.dependsOn processResources

    repositories {
        mavenCentral()
    }

    dependencies {
        // spring
        compile('org.springframework.boot:spring-boot-starter-actuator')
        compile('org.springframework.boot:spring-boot-actuator-docs')
        compile('org.springframework.boot:spring-boot-starter-aop')
        compile('org.springframework.boot:spring-boot-devtools')
        compile('org.springframework.boot:spring-boot-starter-mail')
        compile('org.springframework.boot:spring-boot-starter-security')
        compile('org.springframework.boot:spring-boot-starter-thymeleaf')
        compile('org.springframework.boot:spring-boot-starter-validation')
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.boot:spring-boot-starter-websocket')

        // extension
        compile('org.projectlombok:lombok:1.16.6')

        // database
        compile('org.seasar.doma.boot:doma-spring-boot-starter:1.0.2')
        compile('mysql:mysql-connector-java:5.1.38')

        // specification
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
// ・・・(省略)

datasourceの設定

筆者はyaml形式が好みなので、application.ymlに書いていきます。

# Spring Core
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/{dbname}
    username: {username}
    password: {password}

# Doma2 Reference URL <https://github.com/domaframework/doma-spring-boot>
doma:
  dialect: mysql
  sql-file-repository: no_cache

MySQLを使用する場合は中括弧の値を環境に合わせて設定してください。他のDBを使用する場合は、driver-class-nameurldoma.dialectなどを環境に合わせて変えてください。特にdomaの設定方法については、こちらに記載があります。

あとの手順

このあとの手順はエンティティクラスを作って、DAOインターフェースを作って、SQL書いていけば使用可能です。githubのREADMEを見れば簡単に分かります。

この記事では上記のREADMEには書いていないけど、やっておかないと連携は出来ない小さなことを書きました。そのうちサンプルプロジェクトなどを使って本記事を修正するかもしれませんし、エンティティクラスやDAOインターフェースの自動生成やトランザクションやログ出力などに関することもそのうち書きたいです。

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
14