首页  

Java注解实例     所属分类 java 浏览量 536
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table {
	String value();
}


import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface FieldInfo { String column(); }
@Table("t_user") public class User{ @FieldInfo(column="user_id") private int id; @FieldInfo(column="user_name") private String name; @FieldInfo(column="user_age") private int age = 0;
import java.lang.annotation.Annotation; import java.lang.reflect.Field; public class Hello { public static void main(String[] args) throws Exception { Class clazz = Class.forName("dyyx.User"); Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } Table table = (Table) clazz.getAnnotation(Table.class); String value = table.value(); System.out.println("table=" + value); String[] fieldNames = new String[]{"id", "name", "age"}; for (String fieldName : fieldNames) { Field field = clazz.getDeclaredField(fieldName); FieldInfo anno = field.getAnnotation(FieldInfo.class); System.out.println(fieldName + "=" + anno.column()); } } }
字段上没有注解时 返回null Field field = clazz.getDeclaredField("info"); FieldInfo anno = field.getAnnotation(FieldInfo.class); // null System.out.println(anno);
https://gitee.com/dyyx/hellocode/tree/master/demo/annotationdemo/

上一篇     下一篇
mybatis plus 常用注解

mybatis 日志实现要点

Aviator使用说明

idea 设置异常断点

经典粤语歌

git pull 报错 HTTP Basic: Access denied