Class clazz = entity.getClass();
//獲取類的指定註解
DBTable dbTable = (DBTable) clazz.getAnnotation(DBTable.class);
for(Field field : clazz.getSuperclass().getDeclaredFields()){
//判斷屬性的註解是否存在
field.isAnnotationPresent(DBColumn.class)
//獲取屬性的指定註解
String annotationName = ((DBColumn) field.getAnnotation(DBColumn.class)).name();
}
java中元註解有四個: @Retention @Target @Document @Inherited;
@Retention:註解的保留位置
@Retention(RetentionPolicy.SOURCE) //註解僅存在於源碼中,在class字節碼文件中不包含
@Retention(RetentionPolicy.CLASS) // 默認的保留策略,註解會在class字節碼文件中存在,但運行時無法獲得,
@Retention(RetentionPolicy.RUNTIME) // 註解會在class字節碼文件中存在,在運行時可以通過反射獲取到
@Target:註解的作用目標
@Target(ElementType.TYPE) //接口、類、枚舉、註解
@Target(ElementType.FIELD) //字段、枚舉的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法參數
@Target(ElementType.CONSTRUCTOR) //構造函數
@Target(ElementType.LOCAL_VARIABLE)//局部變量
@Target(ElementType.ANNOTATION_TYPE)//註解
@Target(ElementType.PACKAGE) ///包
@Document:說明該註解將被包含在javadoc中
@Inherited:說明子類可以繼承父類中的該註解
/** ID **/
@DBColumn(name="dicid")
private Integer dicid;
/** 字典類型 **/
@DBColumn(name="dictype")
private Integer dictype;
/** 字典名稱 **/
@DBColumn(name="dicname")
private String dicname;
/** 編碼 **/
@DBColumn(name="dicno")
private String dicno;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DBColumn{
/** 數據庫字段名稱 **/
String name();
}