3
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?

jooq-codegen-gradleで自動生成したコードを利用する

Last updated at Posted at 2025-10-31

はじめに

以前Spring BootでjOOQを利用するという記事を投稿しました。
今回はjooq-codegen-gradleを利用して自動生成したコードを使って、よりタイプセーフなコードにしたいと思います。

build.gradle.kts

次のように記述します。
利用するDBはPostgreSQLで、スキーマはmainとしています。

import org.gradle.kotlin.dsl.jooq

plugins {
	kotlin("jvm") version "1.9.25"
	kotlin("plugin.spring") version "1.9.25"
	id("org.springframework.boot") version "3.5.4"
	id("io.spring.dependency-management") version "1.1.7"
    id("org.jooq.jooq-codegen-gradle") version "3.19.24"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
description = "Demo project for Spring Boot"

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(21)
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-jooq")
    implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	runtimeOnly("org.postgresql:postgresql:42.7.8")
    jooqCodegen("org.postgresql:postgresql:42.7.8")
	testImplementation("org.springframework.boot:spring-boot-starter-test")
	testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
	testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

sourceSets {
    main {
        java {
            srcDirs("build/generated-sources/jooq")
        }
    }
}

jooq {
    configuration {
        jdbc {
            username = "postgres"
            password = "secret"
            driver = "org.postgresql.Driver"
            url = "jdbc:postgresql://localhost:5432/sample"
        }

        generator {
            name = "org.jooq.codegen.DefaultGenerator"
            database {
                name = "org.jooq.meta.postgres.PostgresDatabase"
                inputSchema = "main"
                includes = ".*"
            }
            target {
                packageName = "com.example.jooqsample.generated"
            }
        }
    }
}

kotlin {
	compilerOptions {
		freeCompilerArgs.addAll("-Xjsr305=strict")
	}
}

tasks.withType<Test> {
	useJUnitPlatform()
}

以下のコマンドを実行してコードを自動生成します。

./gradlew :jooqCodegen

これでbuild/generated-sources/jooq 以下にコードが自動生成されます。

実装

以前のコードから次のように修正しました。

@Service
class BookService(
    private val dsl: DSLContext
) {
    fun getBooks(): List<Book> {
        return dsl.select(
            BOOK.ISBN,
            BOOK.TITLE,
            BOOK.AUTHOR,
            BOOK.PUBLISHER,
            BOOK.PRICE
        )
            .from(BOOK)
            .fetchInto(BookRecord::class.java)
            .map {
                it.toEntity()
            }
    }

    private fun BookRecord.toEntity(): Book {
        return Book(
            isbn = isbn,
            title = title,
            author = author,
            publisher = publisher,
            price = price
        )
    }
}

自動生成したコードを利用して、よりタイプセーフなコードに修正することができました!

終わりに

jOOQの自動生成機能を利用してタイプセーフなDBアクセスの方法を記事にしました。
今後も実用的な利用方法について記事にしていけたらと思います。

3
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
3
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?