注解
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
代码生成器支持在生成的代码上添加一组注解,可以使用以下标志启用。 这些注解包括
-
Generated 注解: 可以将 JDK generated 注解添加到所有生成的类,以包含一些有用的元信息,例如 jOOQ 版本、schema 版本或生成日期。 根据配置的
generatedAnnotationType
,注解是以下之一-
javax.annotation.processing.Generated
(JDK 9+) -
javax.annotation.Generated
(JDK 8-)
-
-
Nullable 注解: 当使用 Kotlin 等替代 JVM 语言时,可能希望在生成的代码中包含一些与可空性相关的提示。 例如,当 jOOQ 遇到可为空的列时,JSR-305
@Nullable
注解可能会警告 Kotlin 用户有关已知的可为空列。@Nonnull
列更棘手,因为有很多原因导致 jOOQRecord
在这样的列中可能包含null
值,例如,当记录在没有任何值的情况下初始化时,或者当记录来自UNION
或OUTER JOIN
时。
如果NOT NULL
列具有DEFAULT
表达式,则它被认为是“可写时可空”,即在成功执行INSERT
语句之前,该值可能为null
。
nullableAnnotationType
和nonnullAnnotationType
配置允许指定替代的、限定的注解名称,而不是下面的 JSR-305 类型。-
javax.annotation.Nullable
当列可为空时 -
javax.annotation.Nonnull
当列不可为空时
额外的nullableAnnotationOnWriteOnlyNullableTypes
配置允许指定是否也应将只写可空列(例如,标识/默认或计算的非空列)标记为可空。 -
-
JPA 注解: 可以在 POJO 和其他工件上生成一组最小的 JPA 注解,以传达可用于代码生成器的类型和元数据信息。 这些注解包括
-
jakarta.persistence.Column
-
jakarta.persistence.Entity
-
jakarta.persistence.GeneratedValue
-
jakarta.persistence.GenerationType
-
jakarta.persistence.Id
-
jakarta.persistence.Index
(JPA 2.1 及更高版本) -
jakarta.persistence.Table
-
jakarta.persistence.UniqueConstraint
org.jooq.impl.DefaultRecordMapper
可以使用某些注解(例如@Column
)将记录映射到 POJO。 -
- 验证注解: 可以将一组 Bean Validation API 注解添加到生成的代码,以传达类型信息。 它们包括jOOQ 不实现验证规范,也不验证您的数据,但是您可以使用第三方工具来读取 jOOQ 生成的验证注解。
- Bean 注解: 可以将一组 JavaBeans 注解添加到生成的代码,以促进与 JavaBeans 规范的互操作性
-
Spring 注解: 可以在 DAOs 上生成一些有用的 Spring 注解,以实现更好的 Spring 集成。 这些包括
-
org.springframework.beans.factory.annotation.Autowired
-
org.springframework.stereotype.Repository
-
org.springframework.transaction.annotation.Transactional
(如果设置了<springDao/>
)
-
-
Kotlin 注解: 在某些情况下可能会生成一些 kotlin 特定的注解。 这些包括
-
set:JvmName
-
管理这些注解生成的标志是
XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration> <generator> <generate> <!-- Possible values for generatedAnnotationType - DETECT_FROM_JDK - JAVAX_ANNOTATION_GENERATED - JAVAX_ANNOTATION_PROCESSING_GENERATED - ORG_JOOQ_GENERATED --> <generatedAnnotation>true</generatedAnnotation> <generatedAnnotationType>DETECT_FROM_JDK</generatedAnnotationType> <generatedAnnotationDate>true</generatedAnnotationDate> <generatedAnnotationJooqVersion>true</generatedAnnotationJooqVersion> <nullableAnnotation>true</nullableAnnotation> <nullableAnnotationOnWriteOnlyNullableTypes>true</nullableAnnotationOnWriteOnlyNullableTypes> <nullableAnnotationType>javax.annotation.Nullable</nullableAnnotationType> <nonnullAnnotation>true</nonnullAnnotation> <nonnullAnnotationType>javax.annotation.Nonnull</nonnullAnnotationType> <jpaAnnotations>true</jpaAnnotations> <jpaVersion>2.2</jpaVersion> <validationAnnotations>true</validationAnnotations> <!-- The springDao flag enables the generation of @Transactional annotations on a generated, Spring-specific DAO --> <springAnnotations>true</springAnnotations> <springDao>true</springDao> <kotlinSetterJvmNameAnnotationsOnIsPrefix>true</kotlinSetterJvmNameAnnotationsOnIsPrefix> <constructorPropertiesAnnotation>true</constructorPropertiesAnnotation> <constructorPropertiesAnnotationOnPojos>true</constructorPropertiesAnnotationOnPojos> <constructorPropertiesAnnotationOnRecords>true</constructorPropertiesAnnotationOnRecords> </generate> </generator> </configuration>
有关更多详细信息,请参见配置 XSD、独立代码生成和maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withGenerator( new Generate() // Possible values for generatedAnnotationType // - DETECT_FROM_JDK // - JAVAX_ANNOTATION_GENERATED // - JAVAX_ANNOTATION_PROCESSING_GENERATED // - ORG_JOOQ_GENERATED .withGeneratedAnnotation(true) .withGeneratedAnnotationType(GeneratedAnnotationType.DETECT_FROM_JDK) .withGeneratedAnnotationDate(true) .withGeneratedAnnotationJooqVersion(true) .withNullableAnnotation(true) .withNullableAnnotationOnWriteOnlyNullableTypes(true) .withNullableAnnotationType("javax.annotation.Nullable") .withNonnullAnnotation(true) .withNonnullAnnotationType("javax.annotation.Nonnull") .withJpaAnnotations(true) .withJpaVersion(2.2) .withValidationAnnotations(true) // The springDao flag enables the generation of @Transactional annotations on a // generated, Spring-specific DAO .withSpringAnnotations(true) .withSpringDao(true) .withKotlinSetterJvmNameAnnotationsOnIsPrefix(true) .withConstructorPropertiesAnnotation(true) .withConstructorPropertiesAnnotationOnPojos(true) .withConstructorPropertiesAnnotationOnRecords(true) )
import org.jooq.meta.jaxb.* configuration { generator { generate { // Possible values for generatedAnnotationType // - DETECT_FROM_JDK // - JAVAX_ANNOTATION_GENERATED // - JAVAX_ANNOTATION_PROCESSING_GENERATED // - ORG_JOOQ_GENERATED isGeneratedAnnotation = true generatedAnnotationType = GeneratedAnnotationType.DETECT_FROM_JDK isGeneratedAnnotationDate = true isGeneratedAnnotationJooqVersion = true isNullableAnnotation = true isNullableAnnotationOnWriteOnlyNullableTypes = true nullableAnnotationType = "javax.annotation.Nullable" isNonnullAnnotation = true nonnullAnnotationType = "javax.annotation.Nonnull" isJpaAnnotations = true jpaVersion = 2.2 isValidationAnnotations = true // The springDao flag enables the generation of @Transactional annotations on a // generated, Spring-specific DAO isSpringAnnotations = true isSpringDao = true isKotlinSetterJvmNameAnnotationsOnIsPrefix = true isConstructorPropertiesAnnotation = true isConstructorPropertiesAnnotationOnPojos = true isConstructorPropertiesAnnotationOnRecords = true } } }
有关更多详细信息,请参见配置 XSD和gradle 代码生成。
configuration { generator { generate { // Possible values for generatedAnnotationType // - DETECT_FROM_JDK // - JAVAX_ANNOTATION_GENERATED // - JAVAX_ANNOTATION_PROCESSING_GENERATED // - ORG_JOOQ_GENERATED generatedAnnotation = true generatedAnnotationType = "DETECT_FROM_JDK" generatedAnnotationDate = true generatedAnnotationJooqVersion = true nullableAnnotation = true nullableAnnotationOnWriteOnlyNullableTypes = true nullableAnnotationType = "javax.annotation.Nullable" nonnullAnnotation = true nonnullAnnotationType = "javax.annotation.Nonnull" jpaAnnotations = true jpaVersion = 2.2 validationAnnotations = true // The springDao flag enables the generation of @Transactional annotations on a // generated, Spring-specific DAO springAnnotations = true springDao = true kotlinSetterJvmNameAnnotationsOnIsPrefix = true constructorPropertiesAnnotation = true constructorPropertiesAnnotationOnPojos = true constructorPropertiesAnnotationOnRecords = true } } }
有关更多详细信息,请参见配置 XSD和gradle 代码生成。
// 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.
反馈
您对此页面有任何反馈吗? 我们很乐意倾听!