打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java高级特性——反射机制(完结)——反射与注解

按照我们的学习进度,在前边我们讲过什么是注解以及注解如何定义,如果忘了,可以先回顾一下https://www.cnblogs.com/hgqin/p/13462051.html。

在学习反射和注解前,首先要练习一个ORM。

练习ORM

1.了解什么是ORM:Object RelationShip Mapping ——> 对象关系映射。

 

 从上图可知:

  1.类和表结构对应。

  2.属性和字段对应。

  3.对象和记录对应。

要求:利用注解和反射完成类和表结构的映射关系。

package test;import java.lang.annotation.Annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.Field;//通过反射操作注解public class Test{    public static void main(String[] args) {        Class c1 = Student.class;                //利用反射获取注解        Annotation[] annotations = c1.getAnnotations();        for(Annotation annotation : annotations) {            System.out.println(annotation);        }                //获取注解的value的值        TableHgqin tableHgqin = (TableHgqin) c1.getAnnotation(TableHgqin.class);        String value = tableHgqin.value();        System.out.println(value);                        //获得类指定的注解        Field[] fields = c1.getDeclaredFields();        for(Field field : fields) {            System.out.println("#字段为: " field);            annotations = field.getAnnotations();            for(Annotation annotation : annotations) {                System.out.println(annotation);            }            //获取注解对应的值            FieldHgqin fieldHgqin = (FieldHgqin)field.getAnnotation(FieldHgqin.class);            System.out.println(fieldHgqin.columnName());            System.out.println(fieldHgqin.type());            System.out.println(fieldHgqin.length());        }            }}@TableHgqin("db_student")class Student{        @FieldHgqin(columnName="db_id",type="int",length=10)    private int id;    @FieldHgqin(columnName="db_name",type="int",length=10)    private String name;    @FieldHgqin(columnName="db_age",type="varchar",length=3)    private int age;        public Student() {        super();        // TODO Auto-generated constructor stub    }    public Student(int id, String name, int age) {        super();        this.id = id;        this.name = name;        this.age = age;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public String toString() {        return "student [id="   id   ", name="   name   ", age="   age   "]";    }    }//类名的注解@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@interface TableHgqin{    String value();}//属性的注解@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@interface FieldHgqin{    String columnName();    String type();    int length();}

 

运行结果为:

 

 几篇文章的所有案例,均需要一个一个练习,多看几次就懂了。

 

 完结!!!

来源:https://www.icode9.com/content-1-727151.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
深入理解Java:注解(Annotation)--注解处理器
java注解应用实例
张龙 Annotation学习笔记
java.lang和java.lang.annotation中实现Annotation的类小结
Java自定义注解
Java深入注解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服