package aaa; import java.lang.annotation.*; import java.lang.reflect.Field; public class test{ public static void main(String[] args) throws ClassNotFoundException { Class<?> u1 = Class.forName("aaa.User"); //获取类的注解 Annotation[] annotations = u1.getAnnotations(); for(Annotation annotation:annotations){ System.out.println(annotation);//@aaa.TableName("user") } //获得注解的value TableName tableName = u1.getAnnotation(TableName.class); String value = tableName.value(); System.out.println(value);//user //获得类属性上的注解 Field[] declaredFields = u1.getDeclaredFields(); for(Field field:declaredFields){ TableField annotation = field.getAnnotation(TableField.class); System.out.println(annotation+":"+annotation.columnName()+"--"+annotation.type()); /* @aaa.TableField(columnName="id", type="int"):id--int @aaa.TableField(columnName="account", type="varchar"):account--varchar @aaa.TableField(columnName="password", type="varchar"):password--varchar */ } } } @TableName("user") class User{ @TableField(columnName = "id", type = "int") private Long id; @TableField(columnName = "account", type = "varchar") private String account; @TableField(columnName = "password", type = "varchar") private String password; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", account='" + account + '\'' + ", password='" + password + '\'' + '}'; } } //类名注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface TableName{ String value(); } //属性注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface TableField{ String columnName(); String type(); }
结合 ORM 框架时,一般会使用注解来标识实体类与数据库表之间的映射关系
package aaa; import java.lang.annotation.*; import java.lang.reflect.Field; // 模拟 ORM 框架中的实体类 @Entity(tableName = "user_table") class User { @Id @Column(name = "id", type = "bigint") private Long id; @Column(name = "account", type = "varchar(50)") private String account; @Column(name = "password", type = "varchar(50)") private String password; // 省略 getter 和 setter 方法 @Override public String toString() { return "User{" + "id=" + id + ", account='" + account + '\'' + ", password='" + password + '\'' + '}'; } } // 注解:实体类 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface Entity { String tableName(); } // 注解:主键 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Id { } // 注解:字段 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Column { String name(); String type(); } // 测试类 public class Test { public static void main(String[] args) throws ClassNotFoundException { Class<?> userClass = Class.forName("aaa.User"); // 获取实体类的注解 Entity entityAnnotation = userClass.getAnnotation(Entity.class); String tableName = entityAnnotation.tableName(); System.out.println("Table Name: " + tableName); // 获取实体类的所有字段 Field[] fields = userClass.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Column.class)) { Column columnAnnotation = field.getAnnotation(Column.class); String columnName = columnAnnotation.name(); String columnType = columnAnnotation.type(); System.out.println("Column: " + columnName + ", Type: " + columnType); } else if (field.isAnnotationPresent(Id.class)) { System.out.println("Primary Key: " + field.getName()); } } } }
输出
Table Name: user_table
Column: id, Type: bigint
Column: account, Type: varchar(50)
Column: password, Type: varchar(50)