Java注解的理解与应用

相信广大的Java开发者在开发过程中都遇到和使用了不少的注解,例如Android的Retrofit框架,后端的Spring全家桶等,都使用了大量的注解。那么注解究竟代表什么意思,注解又是如何工作的呢?

可以这样简单的去理解注解:注解它不是程序本身,但可以对程序做出解释,更重要的是可以被其他程序(如编译器等)读取

关于Java内置的三个注解,Override等,这里就不作介绍了,通过一个小例子,来体验一下自定义注解的应用。

在一些数据库框架里,通过注解可以将我们常用的JavaBean映射成数据库的表,下面我们来模拟一下这个过程。

首先,新建Student实体类

public class Student {

    private int id;

    private String name;

    private int 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;
    }
}

接着,新建两个注解类,用来标记表名和列名

@interface表示注解

String value(); 这里的value()不是方法,而是参数,需要注意一下,刚开始接触可能会有些不习惯

表名注解:

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

列名注解:

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

有了这两个注解之后,将它们应用在Student实体类上

@Table("student_table")
public class Student {

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

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

    @StudentField(columnName = "age", type = "int", length = 3)
    private int 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;
    }
}

 

最后,在需要调用的地方,通过反射的方式拿到注解的属性,然后建表

public class Client {
    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("javatest.annotiontest.Student");

            //获得类的所有有效注解
            Annotation[] annotations = clazz.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }

            //获取类的指定注解
            Table table = (Table) clazz.getAnnotation(Table.class);
            System.out.println(table.value());

            //获取类的属性注解
            Field field = clazz.getDeclaredField("name");
            StudentField studentField = field.getAnnotation(StudentField.class);
            System.out.println(studentField.columnName() + " " + studentField.type() + "  " + studentField.length());

            //根据得到的属性创建数据库表

        } catch (ClassNotFoundException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值