使用反射获取注解信息

1.https://www.bilibili.com/video/BV1p4411P7V3
根据狂神视频记录。

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

public class Test1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> aClass = Class.forName("com.sfw.prac.reflect.Student");
        
        TableAnno annotation = aClass.getAnnotation(TableAnno.class);
        System.out.println(annotation);
        System.out.println(annotation.value());

        System.out.println("==========================");
        Field name = aClass.getDeclaredField("name");
        FieldAnno declaredAnnotation = name.getDeclaredAnnotation(FieldAnno.class);
        System.out.println(declaredAnnotation);
        System.out.println(declaredAnnotation.columnName());
        System.out.println(declaredAnnotation.type());
        System.out.println(declaredAnnotation.length());

        System.out.println("==========================");
        Field id = aClass.getDeclaredField("id");
        FieldAnno declaredAnnotation1 = id.getDeclaredAnnotation(FieldAnno.class);
        System.out.println(declaredAnnotation1);
        System.out.println(declaredAnnotation1.columnName());
        System.out.println(declaredAnnotation1.type());
        System.out.println(declaredAnnotation1.length());

        System.out.println("==========================");
        Field age = aClass.getDeclaredField("age");
        FieldAnno declaredAnnotation2 = age.getDeclaredAnnotation(FieldAnno.class);
        System.out.println(declaredAnnotation2);
        System.out.println(declaredAnnotation2.columnName());
        System.out.println(declaredAnnotation2.type());
        System.out.println(declaredAnnotation2.length());
    }
}

@TableAnno("db_student")
class Student{

    @FieldAnno(columnName = "db_name", type = "varchar", length = 10)
    private String name;

    @FieldAnno(columnName = "db_id", type = "int", length = 10)
    private int id;

    @FieldAnno(columnName = "db_age", type = "int", length = 3)
    private int age;

    public Student() {
    }

    public Student(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableAnno {
    String value();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldAnno {
    String columnName();
    String type();
    int length();
}

2.自定义数据校验注解:https://mp.weixin.qq.com/s/gdYysBB3aD_HmPyvEThFXw

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值