LoginSignup
0
0

Doma2を使ってみる(その2)

Last updated at Posted at 2024-04-01

Doma2を使ってみる(その2)

セットアップ

環境

  • 言語
    • Kotlin 1.9.22
  • フレームワーク
    • SpringBoot 3.2.3
  • buildツール
    • Gradle 8.5
  • IDE
    • Intellij

Intellij用プラグイン

Doma Support

IntelliJ上で、Domaを使った開発を少しだけ便利にすることができるプラグイン

  • DAO関連で主にSQLファイルの作成と移動などが簡単になる
    README

Gradle

build.gradle.kts

build.gradle.kts
plugins {
	// KotlinでSpring Boot開発を行うためのプラグイン
	// https://spring.io/guides/tutorials/spring-boot-kotlin/
	id("org.springframework.boot") version "3.2.3"
	id("io.spring.dependency-management") version "1.1.4"
	kotlin("jvm") version "1.9.22"
	kotlin("plugin.spring") version "1.9.22"

	// doma利用のためのプラグイン
	id("org.domaframework.doma.compile") version "2.0.0"
	kotlin("kapt") version "1.9.22"
}

repositories {
	mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
	implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
	implementation("org.jetbrains.kotlin:kotlin-re[build.gradle.kts](..%2FDownloads%2Fdemo%203%2Fbuild.gradle.kts)flect")
	implementation("jakarta.validation:jakarta.validation-api")
	implementation("org.springframework.boot:spring-boot-starter-validation")
	testImplementation("org.springframework.boot:spring-boot-starter-test")
	runtimeOnly("com.h2database:h2")
	
	// doma
	implementation("org.seasar.doma.boot:doma-spring-boot-starter:1.7.0")
	implementation("org.seasar.doma:doma-kotlin:2.56.0")
	kapt("org.seasar.doma:doma-processor:2.56.0")
}

springbootとKotlinとDomaの条件で利用する場合、必要な設定が上記のようになります。
springbootでKotlinを使う場合に必要なものなどは、こちらを参照するまたは、
Spring Initializrでプロジェクトを作成すると、必要な設定が自動で追加される。

ポイント1

org.domaframework.doma.compileプラグインを使うことで、Domaのコンパイル時の処理を行うことができる。
これによって、Domaの設定ファイルなどをアノテーションプロセッサが読み取ることができる。

ポイント2

DomaをKotlinで使用する場合はJavaとは設定が異なります。

  • doma-kotlin
  • doma-processor

README

ポイント3

org.seasar.doma.boot:doma-spring-boot-starter
springbootとDomaを組み合わせるためのスターター

中身は以下

  • spring-boot-starter-data-jdbc
  • spring-boot-starter
  • doma-spring-boot-autoconfigure
    initializrで作成したプロジェクトだった場合、すでに依存関係に追加されているため、必要なのはdoma-spring-boot-autoconfigureだけ。
    あと、doma-spring-boot-starterの最新バージョンが2024年4月現在、1.7.0でspringbootのバージョンが2.7.6なので古いものをみているので、これ単体で使うと古いバージョンになる。
    GitHub

機能は以下

  • Domaの設定とSpring Bootの設定の統合:Spring Bootの設定ファイル(application.propertiesやapplication.yml)を使用してDomaの設定を行うことができます。
  • DomaのDaoクラスの自動スキャン:Spring Bootのコンポーネントスキャン機能を利用して、DomaのDaoクラスを自動的に認識し、DIコンテナに登録します。
  • トランザクション管理の統合:Springのトランザクション管理機能とDomaのトランザクション管理機能を統合し、一貫したトランザクション管理を行うことができます。
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