可用版本: Dev (3.21) | 最新版本 (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14

KotlinGenerator

适用于 ✅ 开源版   ✅ 专业版   ✅ 企业版

jOOQ 可以生成 Kotlin 代码而不是 Java 代码,这允许在生成的代码中利用一些 Kotlin 语言特性。

为了使用 KotlinGenerator,只需将以下类引用放入您的代码生成配置中

XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <name>org.jooq.codegen.KotlinGenerator</name>
  </generator>
</configuration>

有关更多详细信息,请参阅 configuration XSD独立代码生成Maven 代码生成

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(new Generator()
    .withName("org.jooq.codegen.KotlinGenerator")
  )

有关更多详细信息,请参阅 configuration XSD程序化代码生成

import org.jooq.meta.jaxb.*


configuration {
  generator {
    name = "org.jooq.codegen.KotlinGenerator"
  }
}

有关更多详细信息,请参阅 configuration XSDGradle 代码生成

configuration {
  generator {
    name = "org.jooq.codegen.KotlinGenerator"
  }
}

有关更多详细信息,请参阅 configuration XSDGradle 代码生成

// The jOOQ-codegen-gradle plugin has been introduced in version 3.19.
// Please use the official plugin instead of the third party plugin that was recommended before.

只要与生成语言无关,大多数代码生成配置都保持不变。但是有一些 Kotlin 特定的配置标志,如下所示

XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <generate>

      <!-- Tell the KotlinGenerator to generate properties in addition to methods for these paths. Default is true. -->
      <implicitJoinPathsAsKotlinProperties>true</implicitJoinPathsAsKotlinProperties>
      <!--  Workaround for Kotlin generating setX() setters instead of setIsX() in byte code for mutable properties called
            <code>isX</code>. Default is true. -->
      <kotlinSetterJvmNameAnnotationsOnIsPrefix>true</kotlinSetterJvmNameAnnotationsOnIsPrefix>
      <!-- Generate POJOs as data classes, when using the KotlinGenerator. Default is true. -->
      <pojosAsKotlinDataClasses>true</pojosAsKotlinDataClasses>

      <!-- Generate non-nullable types on POJO attributes, where column is not null. Default is false. -->
      <kotlinNotNullPojoAttributes>false</kotlinNotNullPojoAttributes>

      <!-- Generate non-nullable types on Record attributes, where column is not null. Default is false. -->
      <kotlinNotNullRecordAttributes>false</kotlinNotNullRecordAttributes>

      <!-- Generate non-nullable types on interface attributes, where column is not null. Default is false. -->
      <kotlinNotNullInterfaceAttributes>false</kotlinNotNullInterfaceAttributes>
      <!-- Generate defaulted nullable POJO attributes. Default is true. -->
      <kotlinDefaultedNullablePojoAttributes>true</kotlinDefaultedNullablePojoAttributes>

      <!-- Generate defaulted nullable Record attributes. Default is true. -->
      <kotlinDefaultedNullableRecordAttributes>true</kotlinDefaultedNullableRecordAttributes>
    </generate>
  </generator>
</configuration>

有关更多详细信息,请参阅 configuration XSD独立代码生成Maven 代码生成

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(
    new Generate()

      // Tell the KotlinGenerator to generate properties in addition to methods for these paths. Default is true.
      .withImplicitJoinPathsAsKotlinProperties(true)

      // Workaround for Kotlin generating setX() setters instead of setIsX() in byte code for mutable properties called
      // <code>isX</code>. Default is true.
      .withKotlinSetterJvmNameAnnotationsOnIsPrefix(true)

      // Generate POJOs as data classes, when using the KotlinGenerator. Default is true.
      .withPojosAsKotlinDataClasses(true)

      // Generate non-nullable types on POJO attributes, where column is not null. Default is false.
      .withKotlinNotNullPojoAttributes(false)

      // Generate non-nullable types on Record attributes, where column is not null. Default is false.
      .withKotlinNotNullRecordAttributes(false)

      // Generate non-nullable types on interface attributes, where column is not null. Default is false.
      .withKotlinNotNullInterfaceAttributes(false)

      // Generate defaulted nullable POJO attributes. Default is true.
      .withKotlinDefaultedNullablePojoAttributes(true)

      // Generate defaulted nullable Record attributes. Default is true.
      .withKotlinDefaultedNullableRecordAttributes(true)
  )

有关更多详细信息,请参阅 configuration XSD程序化代码生成

import org.jooq.meta.jaxb.*


configuration {
  generator {
    generate {

      // Tell the KotlinGenerator to generate properties in addition to methods for these paths. Default is true.
      isImplicitJoinPathsAsKotlinProperties = true

      // Workaround for Kotlin generating setX() setters instead of setIsX() in byte code for mutable properties called
      // <code>isX</code>. Default is true.
      isKotlinSetterJvmNameAnnotationsOnIsPrefix = true

      // Generate POJOs as data classes, when using the KotlinGenerator. Default is true.
      isPojosAsKotlinDataClasses = true

      // Generate non-nullable types on POJO attributes, where column is not null. Default is false.
      isKotlinNotNullPojoAttributes = false

      // Generate non-nullable types on Record attributes, where column is not null. Default is false.
      isKotlinNotNullRecordAttributes = false

      // Generate non-nullable types on interface attributes, where column is not null. Default is false.
      isKotlinNotNullInterfaceAttributes = false

      // Generate defaulted nullable POJO attributes. Default is true.
      isKotlinDefaultedNullablePojoAttributes = true

      // Generate defaulted nullable Record attributes. Default is true.
      isKotlinDefaultedNullableRecordAttributes = true
    }
  }
}

有关更多详细信息,请参阅 configuration XSDGradle 代码生成

configuration {
  generator {
    generate {

      // Tell the KotlinGenerator to generate properties in addition to methods for these paths. Default is true.
      implicitJoinPathsAsKotlinProperties = true

      // Workaround for Kotlin generating setX() setters instead of setIsX() in byte code for mutable properties called
      // <code>isX</code>. Default is true.
      kotlinSetterJvmNameAnnotationsOnIsPrefix = true

      // Generate POJOs as data classes, when using the KotlinGenerator. Default is true.
      pojosAsKotlinDataClasses = true

      // Generate non-nullable types on POJO attributes, where column is not null. Default is false.
      kotlinNotNullPojoAttributes = false

      // Generate non-nullable types on Record attributes, where column is not null. Default is false.
      kotlinNotNullRecordAttributes = false

      // Generate non-nullable types on interface attributes, where column is not null. Default is false.
      kotlinNotNullInterfaceAttributes = false

      // Generate defaulted nullable POJO attributes. Default is true.
      kotlinDefaultedNullablePojoAttributes = true

      // Generate defaulted nullable Record attributes. Default is true.
      kotlinDefaultedNullableRecordAttributes = true
    }
  }
}

有关更多详细信息,请参阅 configuration XSDGradle 代码生成

// The jOOQ-codegen-gradle plugin has been introduced in version 3.19.
// Please use the official plugin instead of the third party plugin that was recommended before.
虽然很想在生成的代码中使用从表定义派生的非空保证,但重要的是要记住,在 SQL 中,有很多原因导致基于声明为 SQL NOT NULL 的列的表达式可以通过使用 SQL 运算符(例如 LEFT JOINUNION 等)变为 NULL。特别是,具有 DEFAULTIDENTITY 表达式的列是“写入时可为空”的,因此 jOOQ 会将其生成为可为空,即使NOT NULL 约束!

反馈

您对此页面有任何反馈吗? 我们很乐意听到您的反馈!

The jOOQ Logo