Java基础学习:注解和反射15( 反射操作注解 )
-
反射操作注解:
-
getAnnotations:
-
-
-
练习ORM:
-
什么是ORM:Object relationship Mapping:对象关系映射
-
类和表结构对应
-
属性和字段对应
-
对象和记录对应
-
-
要求:利用注解和反射完成类和表结构的映射关系:
-
通过反射自动创建表;(重点)
/**
* 练习:反射操作注解
*/
public class Test12 {
public static void main(String[] args) throws Exception {
Class<?> c1 = Class.forName("com.demo.demo03.Student");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的value的值
TableDemo tableDemo = (TableDemo)c1.getAnnotation(TableDemo.class);
String value=tableDemo.value();
System.out.println(value);//db_student
//获得类指定的注解
Field f = c1.getDeclaredField("name");
FieldDemo fd=(FieldDemo)f.getAnnotation(FieldDemo.class);
System.out.println(fd.columnName());
System.out.println(fd.length());
System.out.println(fd.type());
}
}
-