Table,用類表示數(shù)據(jù)庫的表
創(chuàng)新互聯(lián)是專業(yè)的華鎣網(wǎng)站建設公司,華鎣接單;提供網(wǎng)站設計、網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行華鎣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
@Target(value= {ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table{
String value();
}
類中的屬性,每個屬性表示一個字段
@Target(value= ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Fields {
String columnName();
String type();
int length();
}
類:
@Table("tb_student")
public class Student {
@Fields(columnName="id",type="int",length=10)
private int id;
@Fields(columnName="studentName",type="varchar",length=10)
private String studentName;
@Fields(columnName="age",type="int",length=3)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
解析器:
public class Deam {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
try {
Class c=Class.forName("cn.sxt.in.Student");
Annotation[] annotation= c.getAnnotations(); //獲得類的所有注解,只是類的,不包括方法等
for(Annotation a:annotation)
{
System.out.println(a);
}
//獲得指定類的注解
Table tb=(Table) c.getAnnotation(Table.class);
System.out.println(tb.value());
//獲得屬性的注解
Field f=c.getDeclaredField("studentName"); //通過內(nèi)容返回屬性
Fields f2=f.getAnnotation(Fields.class); //通過屬性返回注解
System.out.println(f2.columnName()+"-->"+f2.type()+"-->"+f2.length());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
本文標題:java反射注解信息
文章源于:http://aaarwkj.com/article36/iggcsg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、企業(yè)網(wǎng)站制作、搜索引擎優(yōu)化、商城網(wǎng)站、品牌網(wǎng)站設計、品牌網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)