注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210

来源:http://www.bjsxt.com/
一、S02E208_01注解_Annotation、内置注解

什么是注解
什么是注解

内置注解
内置注解
SuppressWarnings

二、S02E209_01自定义注解

自定义注解
自定义注解1

元注解
元注解

元注解1
元注解2

package com.test.annotation;

@MyAnnotation01
public class Demo01 {
    @MyAnnotation01(age=19,studentName="test",id=1001,
            schools={"北大","浙大"})
    public static void main(String[] args) {
    }

    @MyAnnotation02(value="aaa")//或者("aaa")
    public void test(){
    }
}
package com.test.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation01 {

    String studentName() default "";
    int age() default 0;
    int id() default -1;

    String[] schools() default{"清华","复旦"};
}
package com.test.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation02 {
    String value();
}

三、S02E210_01反射机制读取注解

注解作业
注解作业

package com.test.annotation2;

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

/**
 * 使用反射读取注解的信息,模拟处理注解信息的流程
 */
public class Demo {
    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("com.test.annotation2.SxtStudent");

            //获取类的所有有效注解
            Annotation[] annotations = clazz.getAnnotations();
            for (Annotation a : annotations) {
                System.out.println(a);
            }
            //获取类的指定的注解
            SxtTable st = (SxtTable) clazz.getAnnotation(SxtTable.class);
            System.out.println(st.value());

            //获取类的属性的注解
            Field f = clazz.getDeclaredField("sname");
            SxtField sxtField = f.getAnnotation(SxtField.class);
            System.out.println(sxtField.columnName()+"--"+sxtField.type()+"--"+sxtField.length());

            //根据获取的表名和字段的信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库中生成相关的表

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package com.test.annotation2;

@SxtTable("tb_student")
public class SxtStudent {

    @SxtField(columnName="id",type="int",length=10)
    private int id;
    @SxtField(columnName="sname",type="varchar",length=10)
    private String sname;
    @SxtField(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 sname;
    }
    public void setStudentName(String studentName) {
        this.sname = studentName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
package com.test.annotation2;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtTable {
    String value();
}
package com.test.annotation2;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtField {
    String columnName();
    String type();
    int length();
}

控制台输出

@com.test.annotation2.SxtTable(value=tb_student)
tb_student
sname--varchar--10
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值