练习:ORM
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3be07b2f70cc9a89d0d93e36dde8f664.png)
代码演示
package com.annotation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.lang.annotation.*;
import java.lang.reflect.Field;
public class Test11 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.annotation.Student");
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
Table table = (Table) c1.getAnnotation(Table.class);
String value = table.value();
System.out.println(value);
Field f = c1.getDeclaredField("name");
FieldKuang annotation = f.getAnnotation(FieldKuang.class);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table("db_student")
class Student{
@FieldKuang(columnName = "db_id",type = "int", length = 10)
private int id;
@FieldKuang(columnName = "db_age",type = "int", length = 10)
private int age;
@FieldKuang(columnName = "db_name",type = "varchar", length = 3)
private String name;
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldKuang{
String columnName();
String type();
int length();
}