使用Java 8出现注释时添加另一个注释

发布于 2025-01-28 13:44:21 字数 854 浏览 4 评论 0原文

我目前正在创建自己的CRUD管理课程。为此,我处理的是这样的注释:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DTO {
    String table() default "";
}

带有此注释注释的每个对象还需要从LOMBOK中使用“ Noargsconstructor” - 宣布,只能应用于类或枚举类型。因此,我不能仅仅将其添加到DTO定义的注释之上。我该如何实现,每个具有DTO通道的班级也可以接收Noargsconstructor,而无需每次手动添加它?

这很好:

@Data
@NoArgsConstructor
@DTO(table = "cast2")
public class CastDTO {
    @Column
    private Integer mid;
    @Column
    private Integer pid;
    @Column
    private String role;
}

但是我宁愿每次创建另一个类时都不必添加noargsconstructor

I am currently creating my own CRUD-managing class. For this purpose I work with an annotation that looks like this:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DTO {
    String table() default "";
}

Every object annotated with this annotation also needs to have the "NoArgsConstructor"-Annotation from lombok, which can only be applied to class or enum types. Therefore I cannot just add it on top of the annotations of the definition of DTO. How can I achieve, that every class that has the DTO-Annotation also receives the NoArgsConstructor without it being required to add it every time manually?

This works fine:

@Data
@NoArgsConstructor
@DTO(table = "cast2")
public class CastDTO {
    @Column
    private Integer mid;
    @Column
    private Integer pid;
    @Column
    private String role;
}

but I'd rather not have to add NoArgsConstructor every time I create another Class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文