orm练习后,我对注解理解的加深

myFirst


我对注解感性的理解

  • 注入信息,解析出信息。
    

有什么用?(只分析此Orm案例)

  •   我觉得注解的主要作用是,在程序运行的时候,
      可以反射将注解中的信息解析出来,最后框架读取注解里的信息
      在框架中进行处理,最终完成业务逻辑。
    

练习反射读取注解信息

  • Orm练习
    练习意义:理解在运行时通过反射将成员变量携带的注解信息提取出来
    

代码

操作:模拟框架获取咋们自定义类中注解的值

//定义注解:类的注解和属性的注解
@Target(ElementType.TYPE)//标签范围
@Retention(RetentionPolicy.RUNTIME)//retention保持时间
//类名的注解
@interface Table{
   String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldji{
   String columnName();
   String type();
   int length();
}

@Table("db_study")
public class student {
   //通过给成员变量“columnName”注入信息,在反射时可以解析出信息
   @Fieldji(columnName = "db_id",length = 10,type = "int")
   private int id;
   //通过给成员变量“name”注入信息,在反射时可以解析出信息
   @Fieldji(columnName = "db_name",length =3,type = "varchar")
   private String name;
   //通过给成员变量“age”注入信息,在反射时可以解析出信息
   @Fieldji(columnName = "db_age",length = 10,type = "int")
   private int age;

   @Override
   public String toString() {
       return "student{" +
               "id=" + id +
               ", name='" + name + '\'' +
               ", 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;
   }
}

执行函数

mport java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class studentTestDemo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {

        //1.通过反射获取注解信息
        Class aClass = Class.forName("cn.itcast.annocation.student");

        //2.class.getAnnotations()只是获得”类“上的注解:@cn.itcast.annocation.Table(value=db_study)
        Annotation[] annotations = aClass.getAnnotations();//an- 加强意义 + -not- 知道

        for(Annotation annocation:annotations){
            System.out.println(annocation);
        }
        //2.1获得”指定名“注解的值
        Table table = (Table)aClass.getAnnotation(Table.class);
        String value=table.value();
        System.out.println(value);//db_study

        //3.class类通过的成员变量被注入的注解
        Field name= aClass.getDeclaredField("name");
        Fieldji field=name.getAnnotation(Fieldji.class);
        //3.1注解及对象,直接获取其中的属性field.columnName()||field.columnName()||field.columnName()
        System.out.println("数据库中的字段名为: "+field.columnName());
        System.out.println("数据库中该字段长度为:"+field.length());
        System.out.println("数据库中的字段类型为:"+field.type());
    }
}

菜鸟的我通过这个案例总结:

  •   框架中使用注解估计就是通过反射获取到该类上所有的量(成员变量,						           	
      方法,类名等)的注解中的信息。
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值