快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java反射注解信息-创新互联

Table,用类表示数据库的表

从事电信机房托管,服务器租用,云主机,雅安服务器托管主机域名,CDN,网络代维等服务。
@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();
    }
}
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网站题目:java反射注解信息-创新互联
网页链接:http://6mz.cn/article/gshsd.html

其他资讯