注解作用
注解(Annotation)相当于一种标记,在程序中加入注解就等于为程序打上某种标记,没有加,则等于没有任何标记,以后,javac编译器、开发工具和其他程序可以通过反射来了解你的类及各种元素上有无何种标记,看你的程序有什么标记,就去干相应的事,标记可以加在包、类,属性、方法,方法的参数以及局部变量上。
-
不是程序本身,可以对程序作出解释。
-
可以被其他程序(如:编译器)读取。
Java SE5内置了三种标准注解:
@Override,表示当前的方法定义将覆盖超类中的方法。
@Deprecated,使用了注解为它的元素编译器将发出警告,因为注解@Deprecated是不赞成使用的代码,被弃用的代码。
@SuppressWarnings,关闭不当编译器警告信息。
自定义注解
Java还提供了4中注解,专门负责新注解的创建。
@Target | 表示该注解可以用于什么地方,可能的ElementType参数有: CONSTRUCTOR:构造器的声明 FIELD:域声明(包括enum实例) LOCAL_VARIABLE:局部变量声明 METHOD:方法声明 PACKAGE:包声明 PARAMETER:参数声明 TYPE:类、接口(包括注解类型)或enum声明 |
@Retention | 表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括: SOURCE:注解将被编译器丢弃 CLASS:注解在class文件中可用,但会被VM丢弃 RUNTIME:VM将在运行期间保留注解,因此可以通过反射机制读取注解的信息。 |
@Document | 将注解包含在Javadoc中 |
@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 MyAnnotation {
//参数类型+参数名
String studentName() default "";
int age() default 0;
int id() default -1;//不存在
String[] schools() default {"abc","bcd"};
}
public class test {
@MyAnnotation(age = 18,id = 4)
public void function(){ }
}
反射机制读取注解实现简单ORM
- 定义类注解-实现类和数据库表名关联
- 定义属性注解-显示类中属性与数据库字段属性关联
- 定义类,添加注解
- 完成解析类,利用反射机制获取注解的属性,拼接为sql语句
定义类注解-实现类和数据库表名关联
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 类与数据库表的转化
* 类与表名对应
* @author 袁盛桐
*/
@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface YstTable {
//只有一个参数,建议其名叫value
String value();
}
定义属性注解-显示类中属性与数据库字段属性关联
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 用来说明表里面的特性
* @author 袁盛桐
*/
@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface YstField {
//属性名字
String columnName();
//数据类型
String type();
//长度
int length();
}
定义类,添加注解
//定义对应的表的名字
@YstTable("tb_student")
public class YstStudent {
//通过注解来定义这个属性在表中的具体属性
@YstField(columnName = "sid",type = "int", length = 10)
private int id;
@YstField(columnName = "sname",type = "varchar", length = 10)
private String studentName;
@YstField(columnName = "sage",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;
}
}
完成解析类,利用反射机制获取注解的属性,拼接为sql语句
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* 使用反射读取注解的信息,模拟处理注解信息的流程
* @author 袁盛桐
*/
public class YstParseAnnotation {
public static void main(String[] args) {
try{
//通过反射获取这个类的所有信息
Class info = Class.forName("YstStudent");
//获取这个类的所有注解
Annotation[] annotations = info.getAnnotations();
for(Annotation a : annotations){
System.out.println(a);
}
//获取类的指定注解
YstTable yt = (YstTable)info.getAnnotation(YstTable.class);
System.out.println(yt.value());
//获得类的属性的注解
Field f = info.getDeclaredField("studentName");
YstField ystField = f.getAnnotation(YstField.class);
System.out.println(ystField.columnName()+"---"+ystField.type()+"---"+ystField.length());
//根据获得的表名字和字段信息,拼出ddl语句,使用jdbc执行这个sql,在数据库中生成相关表
}catch (Exception e){
e.printStackTrace();
}
}
}