如何实现自定义类字段的注释并实施保存服务方法以清理类的字段

发布于 2025-01-20 09:50:09 字数 1292 浏览 2 评论 0原文

我有三个类,

public class A {
  private int id;
  private String name;
  private int validate;
}

public class B {
  private int id;
  private LocalDateTime modifiyTime;
}

public class C {
  private int id;
  private int sequence;
}

我想要在服务层上更多的抽象,因此设计了baseService

public interface BaseService<T> {
  default void saveCheck(T t) {
      clear(t);
      duplicateCheck(t);
      preProcess(t);
      if(save(t)) {
        postProcess(t);
      }
  }
  
  void duplicateCheck(T t);
  
  void clear(T t);

  void preProcess(T t);

  boolean save(T t);

  void postProcess(T t);
}

我想设计注释@clearfiled,在实体的字段上标记,这样的

public class A {
  @ClearFiled
  private int id;
  private String name;
  @ClearFiled
  private int validate;
}

效果是替换以下代码

public interface IAService extends BaseService<A> {

}

public class AServiceImpl extends IAService {
  // @ClearFiled is used to omit this part of the code
  // this method belong to BaseService
  @Override
  public void clear(A a) {
    a.setId(0);
    a.setValidate(0);
  }
}

。我要这样做吗?

I have three Class

public class A {
  private int id;
  private String name;
  private int validate;
}

public class B {
  private int id;
  private LocalDateTime modifiyTime;
}

public class C {
  private int id;
  private int sequence;
}

I want more abstract at the Service layer, so BaseService was designed

public interface BaseService<T> {
  default void saveCheck(T t) {
      clear(t);
      duplicateCheck(t);
      preProcess(t);
      if(save(t)) {
        postProcess(t);
      }
  }
  
  void duplicateCheck(T t);
  
  void clear(T t);

  void preProcess(T t);

  boolean save(T t);

  void postProcess(T t);
}

I want to design an annotation @ClearFiled, marked on the entity's field, like this

public class A {
  @ClearFiled
  private int id;
  private String name;
  @ClearFiled
  private int validate;
}

Its effect is to replace the following code

public interface IAService extends BaseService<A> {

}

public class AServiceImpl extends IAService {
  // @ClearFiled is used to omit this part of the code
  // this method belong to BaseService
  @Override
  public void clear(A a) {
    a.setId(0);
    a.setValidate(0);
  }
}

How do i do this?

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

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

发布评论

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