Annotation definition use ---orm mapping annotation use a brief introduction

 

 

1 Introduction to annotations:

   a) Existence meaning: After the definition, it is used to be read by the compiler and served on the class that uses the annotation

   b) Define and use steps:

          b.1) Define annotations

          b.2) Declare the reference annotation in the target class

          b.3) Define the interpreter class to interpret annotations and process annotations

 

 

2 The detailed concept of annotation is shown in the following figure:

 

 

 

 



 

 

3 Annotations are simply defined and used:

 

1 Define annotations

@Target(value={ElementType.METHOD,ElementType.TYPE}) // Specify the scope for methods and classes
@Retention(RetentionPolicy.RUNTIME)// Specify the valid range (soruce + class level is valid and supports reflection)
public @interface SxtAnnotation01 {
	// When defining an attribute, it is basically necessary to have a default value, otherwise an error will be reported when using the annotation, so that there is an initial value. The initial value is generally an empty string or 0. If it is -1, it means that it does not exist.
	String studentName() default "";
	int age() default 0;
	int id() default -1;   //String indexOf("abc")  -1
	
	String[] schools() default {"Tsinghua University","Beijing Shangxuetang"};
}


2 Using annotations

@SxtAnnotation01 // use annotations on classes
public class Demo02 {
	
	@SxtAnnotation01(age=19,studentName="Lao Gao",id=1001,
			schools={"Peking University","Beijing University of Aeronautics and Astronautics"})
	public void test(){ // use annotation on method and assign value
	}
	
	
	
}

 

 

4 Annotations use simple writing on orm:

 



 

 

 

1 Define table annotations

@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TableAnno {
	String value();
}

2 Define attribute annotations

@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnno {
	String columnname();
	
	String type();
	
	int length();
	
}


3 Reference annotations on entity beans

@TableAnno(value = "student_table")
public class Student {

	@FieldAnno(columnname = "id", length = 10, type = "int")
	private int id;
	@FieldAnno(columnname = "name", length = 10, type = "varchar")
	private String name;
	@FieldAnno(columnname = "age", length = 3, type = "int")
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
}


4 Define the parsing processing annotation class

public class AnalyticalAnnocation {

	/**
	 * Parse custom annotations
	 */
	public static void main(String[] args) {

		try {
			Class<Student> clazz = (Class<Student>) Class.forName("zm.annocation.Student");
			// Get the attribute value of the class annotation TableAnno
			TableAnno tableAnno = clazz.getAnnotation(TableAnno.class);
			System.out.println(tableAnno.value());  // student_table
			// get property annotation
			Field  field = clazz.getDeclaredField("id");
			FieldAnno fieldAnno = field.getAnnotation(FieldAnno.class);
			int idLen = fieldAnno.length();
			String idType = fieldAnno.type();
			String idName = fieldAnno.columnname();
			System.out.println(idName + "--" + idType + "--" + idLen );//id--int--10
			
			
			Field  field1 = clazz.getDeclaredField("name");
			FieldAnno fieldAnno1 = field1.getAnnotation(FieldAnno.class);
			int nameLen = fieldAnno1.length();
			String nameType = fieldAnno1.type();
			String nameName = fieldAnno1.columnname();
			System.out.println(nameName + "--" + nameType + "--" + nameLen );//name--varchar--10
			
			
			Field  field2 = clazz.getDeclaredField("age");
			FieldAnno fieldAnno2 = field2.getAnnotation(FieldAnno.class);
			int ageLen = fieldAnno2.length();
			String ageType = fieldAnno2.type();
			String ageName = fieldAnno2.columnname();
			System.out.println(ageName + "--" + ageType + "--" + ageLen );//age--int--3
			
			
			// The above operation obtains the corresponding representation of the entity Student, the table field name, type, length, and the following can be created by splicing. Query, and other SQL operations
			// ........ slightly
			
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

}

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326370897&siteId=291194637