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

生成的 POJO

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

数据库中的每个表和视图都会生成一个类似于这样的 POJO 实现

// JPA annotations can be generated, optionally
@Entity
@Table(name = "BOOK", schema = "TEST")
public class Book implements java.io.Serializable

// An interface common to records and pojos can be generated, optionally
, IBook {

    // JSR-303 annotations can be generated, optionally
    @NotNull
    private Integer id;

    @NotNull
    private Integer authorId;

    @NotNull
    @Size(max = 400)
    private String title;

    // Every column generates a getter and a setter
    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 7)
    @Override
    public Integer getId() {
        return this.id;
    }

    @Override
    public void setId(Integer id) {
        this.id = id;
    }

    // [...]
}
XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <generate>
      <!-- Allows for turning on POJOs generation: default false -->
      <pojos>true</pojos>

      <!-- Optionally, limit POJOs generation to only tables matching this regular expression. -->
      <pojosIncludes>.*</pojosIncludes>

      <!-- Optionally, limit POJOs generation to only tables not matching this regular expression.
           Excludes match before includes. -->
      <pojosExcludes>SYSTEM_TABLE_.*</pojosExcludes>
    </generate>
  </generator>
</configuration>

有关更多详细信息,请参见 配置 XSD独立代码生成maven 代码生成

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

      // Allows for turning on POJOs generation: default false
      .withPojos(true)

      // Optionally, limit POJOs generation to only tables matching this regular expression.
      .withPojosIncludes(".*")

      // Optionally, limit POJOs generation to only tables not matching this regular expression.
      // Excludes match before includes.
      .withPojosExcludes("SYSTEM_TABLE_.*")
  )

有关更多详细信息,请参见 配置 XSD编程代码生成

import org.jooq.meta.jaxb.*


configuration {
  generator {
    generate {

      // Allows for turning on POJOs generation: default false
      isPojos = true

      // Optionally, limit POJOs generation to only tables matching this regular expression.
      pojosIncludes = ".*"

      // Optionally, limit POJOs generation to only tables not matching this regular expression.
      // Excludes match before includes.
      pojosExcludes = "SYSTEM_TABLE_.*"
    }
  }
}

有关更多详细信息,请参见 配置 XSDgradle 代码生成

configuration {
  generator {
    generate {

      // Allows for turning on POJOs generation: default false
      pojos = true

      // Optionally, limit POJOs generation to only tables matching this regular expression.
      pojosIncludes = ".*"

      // Optionally, limit POJOs generation to only tables not matching this regular expression.
      // Excludes match before includes.
      pojosExcludes = "SYSTEM_TABLE_.*"
    }
  }
}

有关更多详细信息,请参见 配置 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.

一如既往,当使用正则表达式时,它们是具有默认标志的正则表达式

XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <generate>
      <!-- Allows for turning on POJOs generation: default false -->
      <pojos>true</pojos>
    </generate>
  </generator>
</configuration>

有关更多详细信息,请参见 配置 XSD独立代码生成maven 代码生成

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

      // Allows for turning on POJOs generation: default false
      .withPojos(true)
  )

有关更多详细信息,请参见 配置 XSD编程代码生成

import org.jooq.meta.jaxb.*


configuration {
  generator {
    generate {

      // Allows for turning on POJOs generation: default false
      isPojos = true
    }
  }
}

有关更多详细信息,请参见 配置 XSDgradle 代码生成

configuration {
  generator {
    generate {

      // Allows for turning on POJOs generation: default false
      pojos = true
    }
  }
}

有关更多详细信息,请参见 配置 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.

影响生成的 POJO 的标志

来自代码生成配置的其他标志会影响生成的 POJO

  • daos:POJO 是 DAO 的前提。如果生成 DAO,则也会生成 POJO
  • dateAsTimestamp:这会影响所有相关的 getter 和 setter
  • immutablePojos:不可变的 POJO 具有 final 成员并且没有 setter。所有成员都必须传递给构造函数
  • interfaces:如果生成接口,则 POJO 将实现它们
  • jpaAnnotations:JPA 注释用于生成的记录(此处有详细信息)
  • jpaVersion:JPA 规范的版本,用于生成特定于版本的注释。 如果省略,则默认使用最新版本。(此处有详细信息)
  • pojosAsJavaRecordClasses:如果您使用的是 JavaGenerator,这将生成 POJO 作为(不可变的)Java 16 记录类型
  • pojosAsScalaCaseClasses:如果您使用的是 ScalaGenerator(或 Scala3Generator),这将生成 POJO 作为(可变或不可变的)Scala case 类
  • pojosAsKotlinDataClasses:如果您使用的是 KotlinGenerator,这将生成 POJO 作为(可变或不可变的)kotlin 数据类
  • pojosToString:POJO 是否应具有生成的 toString() 实现。
  • pojosEqualsAndHashCode:POJO 是否应具有生成的 equals()hashCode() 实现。 这些实现纯粹是基于值的,就像记录一样,即如果所有属性都相等,则两个 POJO 相等。
  • pojosEqualsAndHashCodeIncludePrimaryKeyOnly:生成的 equals()hashCode() 实现是否应考虑主键列(请注意,该实现仍然是纯粹基于值的,即两个未初始化的 null 主键值被认为是相等的)。
  • pojosEqualsAndHashCodeColumnIncludeExpression:一个正则表达式,匹配应包含在生成的 equals()hashCode() 实现中的限定或非限定列名。
  • pojosEqualsAndHashCodeColumnExcludeExpression:一个正则表达式,匹配应从生成的 equals()hashCode() 实现中排除的限定或非限定列名。
  • unsignedTypes:这会影响所有相关的 getter 和 setter
  • validationAnnotations:JSR-303 验证注释用于生成的记录(此处有详细信息)

引用此页

反馈

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

The jOOQ Logo