反射机制读取自定义注解

来源:互联网 发布:捷速pdf文字识别软件 编辑:程序博客网 时间:2024/06/03 12:40

自定义注解类的类

package com.dasenlin.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 自定义学生类的注解仅修饰属性注解类 * @author Administrator * */@Target(value={ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface DslTable {    String value();//修饰时需要用到的参数}

自定义注解类的属性的类

package com.dasenlin.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 自定注解属性 * @author Administrator * */@Target(value={ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)public @interface DslFiled {    String columnName();    String type();    int length();}

注解的实体类

package com.dasenlin.annotation;@DslTable("tb_student")public class Student {    @DslFiled(columnName="sid",type="int",length=10)    private Integer sid;    @DslFiled(columnName="sname",type="varchar2",length=10)    private String sname;    @DslFiled(columnName="sage",type="int",length=3)    private Integer sage;    public Student() {        super();    }    public Student(Integer sid, String sname, Integer sage) {        super();        this.sid = sid;        this.sname = sname;        this.sage = sage;    }    public Integer getSid() {        return sid;    }    public void setSid(Integer sid) {        this.sid = sid;    }    public String getSname() {        return sname;    }    public void setSname(String sname) {        this.sname = sname;    }    public Integer getSage() {        return sage;    }    public void setSage(Integer sage) {        this.sage = sage;    }}

反射机制获取实体类中所有信息

package com.dasenlin.annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Field;/** * 模拟注解读取流程, * @author Administrator * 通过反射机制 */public class LoadStudent {    public static void main(String[] args) {        try {            Class clazz=Class.forName("com.dasenlin.annotation.Student");//反射机制获取类的所有私有非私有属性包括注解            //1.获取类所有的注解@com.dasenlin.annotation.DslTable(value=tb_student)            Annotation [] annotations=clazz.getAnnotations();            for(Annotation annta:annotations){                System.out.println(annta);//这个类的注解            }            //2.获得类的指定的注解tb_student            DslTable dslt =(DslTable) clazz.getAnnotation(DslTable.class);//多个注解直接获取一个指定的注解类            System.out.println(dslt.value());//直接获取这个注解在该类中的值            //3.获取类的属性sname的注解sname:varchar2:10            Field f=clazz.getDeclaredField("sname");//获取注解的属性名            DslFiled dslf=f.getAnnotation(DslFiled.class);            System.out.println(dslf.columnName()+":"+dslf.type()+":"+dslf.length());            //4.根据获得的表的名称,字段的信息。拼出DDl语句,然后只用jdbc执行sql语句,在数据库中生成表,hibernate和spring后台部分的基本思路        } catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {            e.printStackTrace();        }    }}
0 0
原创粉丝点击