java反射注解信息

Table,用类表示数据库的表

创新互联是专业的华蓥网站建设公司,华蓥接单;提供网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行华蓥网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

@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"); //通过内容返回属性
        Fields f2=f.getAnnotation(Fields.class); //通过属性返回注解
        System.out.println(f2.columnName()+"-->"+f2.type()+"-->"+f2.length());

    } catch (ClassNotFoundException e) {

        e.printStackTrace();
    }
}
}

本文标题:java反射注解信息
文章源于:https://www.cdcxhl.com/article36/iggcsg.html

成都网站建设公司_创新互联,为您提供全网营销推广企业网站制作搜索引擎优化商城网站品牌网站设计品牌网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司