了解注解
java.lang.Annotation是从JDK5.0开始引入的新技术
注解定义:
(1)注解Annotation:用于描述代码,说明程序,主要目的是为了给计算机看,且能够影响程序的运行,可以被其他程序读取。 (2)注释comment:用于描述代码的作用和一些关键性的知识点,使用文字描述程序,是为了给程序员观看,以此来使程序员能够以快的时间了解被注释的代码。
注解格式:
以“@注释名”在代码中存在的,还可以添加一些参数值,例如:@SuppressWarnings(value=“unchecked”);
注解使用:
可以附加在package,class,interface,method,field等上边,相当于给他们添加了额外的辅助信息,我们可以通过反射机制编程实现对这些元数据的访问
内置注解
@Override 覆盖,重写
@Deprecated 弃用,不推荐,但可以用
@SuppressWarnings 镇压警告 ,用来抑制编译时的警告信息,与前两个注解不同,这个注解需要添加一个参数才能使用,参数都定义好了,供我们选择
- @SuppressWarnings (“all”)
- @SuppressWarnings(“unchecked”)
- @SuppressWarnings(value={“unchecked”,“deprecation”})
- …
元注解
元注解的作用是注解其他注解,Java定义了4个标准的meta-annotation类型,他们提供给其他注解类型说明
- @Target:用于描述注解的使用范围
- @Retention:表示需要在什么级别保存该注解的信息,用于描述的生命周期(source<class<runtime)
- @Document:说明该注解将被包含在JavaDoc中
- @Inherited:说明子类可以继承父类中的注解
package com.xm;
import java.lang.annotation.*;
/**
* 注解测试类
*/
public class Demo1 {
public static void main(String[] args) {
test();
}
@MyAnnotation
public static void test()
{
System.out.println("测试完成");
}
}
/**
* 定义注解
*/
//1.Target 注解能用在什么地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})
//2.Retention 注解在什么地方还有效 (Runtime>class>source)
@Retention(value = RetentionPolicy.RUNTIME)
//3.Documented 表示是否将我们的注解生成在JavaDoc中
@Documented
//4.Inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation{
}
自定义注解
使用@interface自定义注解时,自动继承了java.lang.Annotation接口
- @interface用来声明一个注解,格式为 public @interface 注解名{自定义内容}
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称
- 返回值类型就是参数的类型(返回值类型只能是基本数据类型,Class,String,enum)
- 可以通过default来声明参数 的默认值
- 如果只用一个参数成员,一般参数名为value
- 注解元素必须要值,我们定义直接元素时,经常使用空字符串,数值0,作为默认值,其中-1,代表不存在
package com.xm;
import java.lang.annotation.*;
//可以显示赋值,如果没有默认值,就必须显式赋值
@MyAnnotation2(name = "肖猛", age = 5, id = 5, address = {"山东", "菏泽", "东明"})
public class Demo2 {
public static void main(String[] args) {
test2();
test3();
}
@MyAnnotation2(name = "肖猛", age = 5, id = 5, address = {"山东", "菏泽", "东明"})
public static void test2() {
}
@MyAnnotation3("无价之宝")
public static void test3() {
}
}
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface MyAnnotation2 {
//注解的参数 参数类型+参数名()
//String name();
//可以定义默认值
String name() default "名字";
int age() default 0;
int id() default -1;//代表不存在
String[] address() default "中国";
}
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface MyAnnotation3 {
String value() default "value";
}
练习ORM
ORM: Object relationship Mapping (对象关系映射)
-
类和表对应
-
属性和字段对应
-
对象和记录对应
利用注解和反射完成类和表结构的映射关系
package com.xm;
import java.lang.annotation.*;
/**
* 反射操作注解
*/
public class Demo3 {
public static void main(String[] args) throws NoSuchFieldException {
Class<Student> c1 = Student.class;
//反射获得注解
Annotation[] annotations = c1.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的Value值
Table table = c1.getAnnotation(Table.class);
String value = table.value();
System.out.println(value);
//获得字段的注解
java.lang.reflect.Field id = c1.getDeclaredField("name");
Field idAnnotation = id.getAnnotation(Field.class);
System.out.println(idAnnotation.columnName());
System.out.println(idAnnotation.Type());
System.out.println(idAnnotation.length());
}
}
/**
* 类名注解
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table {
String value() default " ";
}
/**
* 属性的注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Field {
String columnName();
String Type();
int length();
}
@Table("db_Student")
class Student {
@Field(columnName = "db_id", Type = "int", length = 10)
private int id;
@Field(columnName = "db_age", Type = "int", length = 10)
private int age;
@Field(columnName = "db_name", Type = "Varchar", length = 5)
private String name;
public Student() {
}
public Student(int id, int age, String name) {
this.id = id;
this.age = age;
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}