注解入门
Annotation作用:
①对程序做出解释(和注释相同)
②可以被其他程序读取(注解信息处理流程)
Annotation的格式:
@注释名,还可以添加一些参数值,eg:@SuppressWarnings(value="unchecked".
Annotation在哪里使用:
可以附加在package,class,method,field等上面,相当于给它们添加了额外的辅助信息,我们可以通过反射机制编程实现对这些元数据的访问。
内置注解
@override 重写
@Deprecated 此注释可用于修辞方法,属性,类,表示不鼓励使用这样的元素,通常是因为他很危险或存在更好的选择
@SuppressWarnings
用来抑制编译时的警告信息。需要添加一个参数才能正确使用,参数如下
-@SuppressWarnings("unchecked")
-@SuppressWarnings(value={"unchecked","deprecation"})
自定义注解、元注解
-@interface用来声明一个注解
格式为:
- public @interface 注解名{定义体}
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称
- 返回值类型就是参数的类型
- 可以通过default来声明参数的默认值
- 如果只有一个参数成员,一般参数名为value
注:注解元素必须要有值,我们定义注解元素时,经常使用空字符串、0作为默认值。也经常使用负数(-1)表示不存在的含义
元注解:
元注解的作用就是负责注解其他注解,有四个元注解
- @Target
作用:用于描述注解的使用范围
@Target(value = ElementType.TYPE)
- @Retention
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期
- @Documented
- @Inherited
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtAnnotation01 {
String studentName() default "";
int age() default 0;
int id() default -1;
String[] schools() default {"SM","JYP"};
}
练习作业:使用注解完成类和表结构的映射关系
package annotation;
@SxtTable("tb_student")
public class SxtStudent {
@SxtField(columnName="id",type="int",length=10)
private int id;
@SxtField(columnName="sname",type="varchar",length=10)
private String studentName;
@SxtField(columnName="age",type="int",length=3)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//修饰类,定义类到表的转化
@Target(value= {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtTable {
String value();
}
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//修饰属性
@Target(value= {ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtField {
String columnName();
String type();
int length();
}
package annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/*
* 使用反射读取注解的信息,模拟处理注解信息的流程
*/
public class Demo {
public static void main(String[] args) {
try {
Class clazz = Class.forName("annotation.SxtStudent");
//获得类的所有有效注解
Annotation[] annotations = clazz.getAnnotations();
for(Annotation a:annotations) {
System.out.println(a);
}
//获得类的指导的注解
SxtTable st = (SxtTable)clazz.getAnnotation(SxtTable.class);
System.out.println(st.value());
//获得类的属性的注解
Field f = clazz.getDeclaredField("studentName");
SxtField sxtField = f.getAnnotation(SxtField.class);
System.out.println(sxtField.columnName()+"--"+sxtField.type()+"--"+sxtField.length());
//根据获得的表名、字段的信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库中生成相关的表
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}