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

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

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

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

@Retention(RUNTIME)
@Target(TYPE) 
public @interface StoredProcedure {
    String query ();
    String structName ();
    String structArrayName ();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) 
public @interface Column {
    
    public String name();
    public int position ();
    public String datePattern () default "MM/dd/yyyy HH:mm:ss.SSSSSS";
    public String defValue () default "";
    public boolean required () default false;
    public String actualType () default "na";
}

// 我们可以在类级别和字段级别应用注解。
@StoredProcedure(query = "{}", structName = "", structArrayName = "")
public class Data {

    @Column(name = "id", position = 1, actualType = "string", required = true)
    @Validate(length = )
    public String id;

    @Column(name = "name", position = 2, actualType = "number")
    @Validate(length = )
    public Long name;
}
英文:

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

@Retention(RUNTIME)
@Target(TYPE) 
public @interface StoredProcedure {
	String query ();
	String structName ();
	String structArrayName ();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) 
public @interface Column {
	
	public String name();
	public int position ();
	public String datePattern () default "MM/dd/yyyy HH:mm:ss.SSSSSS";
	public String defValue () default "";
	public boolean required () default false;
	public String actualType () default "na";
}

// we can apply annotations in class level and field level.
@StoredProcedure(query = "{}", structName = "", structArrayName = "")
public class Data {

	@Column(name = "id", position = 1, actualType = "string", required = true)
	@Validate(length = )
	public String id;

	@Column(name = "name", position = 2, actualType = "number")
	@Validate(length = )
	public Long name;
}

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:

确定