How to create custom annotation for StoredProcedure and Column in spring boot project?

huangapple go评论95阅读模式
英文:

How to create custom annotation for StoredProcedure and Column in spring boot project?

问题

我需要为StoredProcedure和Column创建自定义注释。
示例:
@StoredProcedure带有三个参数,如query、structname、structArrayName
@Column有五个字段,如name、position、type、required。

有人可以帮我学习如何创建这些注释并应用于实体类吗?

英文:

I need to creat custom annotations for StoredProcedure & Column.
Example:
@StoredProcedure with three parameters like query,structname,structArrayName
and @Column with have five fields like name, postion, type, required.

Can anybody help me how create it and apply in entity class?

答案1

得分: -1

我们可以添加元注解来指定自定义注解的作用范围和目标。例如:

  1. @Retention(RUNTIME)
  2. @Target(TYPE)
  3. public @interface StoredProcedure {
  4. String query ();
  5. String structName ();
  6. String structArrayName ();
  7. }
  8. @Retention(RetentionPolicy.RUNTIME)
  9. @Target(ElementType.FIELD)
  10. public @interface Column {
  11. public String name();
  12. public int position ();
  13. public String datePattern () default "MM/dd/yyyy HH:mm:ss.SSSSSS";
  14. public String defValue () default "";
  15. public boolean required () default false;
  16. public String actualType () default "na";
  17. }
  18. // 我们可以在类级别和字段级别应用注解。
  19. @StoredProcedure(query = "{}", structName = "", structArrayName = "")
  20. public class Data {
  21. @Column(name = "id", position = 1, actualType = "string", required = true)
  22. @Validate(length = )
  23. public String id;
  24. @Column(name = "name", position = 2, actualType = "number")
  25. @Validate(length = )
  26. public Long name;
  27. }
英文:

We can add meta-annotations to specify the scope and the target of our custom annotation.
Ex:

  1. @Retention(RUNTIME)
  2. @Target(TYPE)
  3. public @interface StoredProcedure {
  4. String query ();
  5. String structName ();
  6. String structArrayName ();
  7. }
  8. @Retention(RetentionPolicy.RUNTIME)
  9. @Target(ElementType.FIELD)
  10. public @interface Column {
  11. public String name();
  12. public int position ();
  13. public String datePattern () default "MM/dd/yyyy HH:mm:ss.SSSSSS";
  14. public String defValue () default "";
  15. public boolean required () default false;
  16. public String actualType () default "na";
  17. }
  18. // we can apply annotations in class level and field level.
  19. @StoredProcedure(query = "{}", structName = "", structArrayName = "")
  20. public class Data {
  21. @Column(name = "id", position = 1, actualType = "string", required = true)
  22. @Validate(length = )
  23. public String id;
  24. @Column(name = "name", position = 2, actualType = "number")
  25. @Validate(length = )
  26. public Long name;
  27. }

huangapple
  • 本文由 发表于 2020年10月27日 15:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64549555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定