Java专题十八:注解
2021-02-07 14:19
                         标签:stringbu   set   element   重写   rri   retention   source   access   constant    枚举类 枚举类 假设我们设计一个数据库Helper类,用于JDBC插入数据,实现类似mybatis的ORM模型 首先定义2个注解,注解 现在有2个 使用反射获取字段的值,使用注解获取数据库中字段的名字,拼接SQL语句 使用如下代码验证: 输出: Java专题十八:注解 标签:stringbu   set   element   重写   rri   retention   source   access   constant    原文地址:https://www.cnblogs.com/myibu/p/12775709.htmlJava专题十八:注解
18.1. 元注解
 
所在包 
类名 
描述 
 
java.lang 
Override表示此方法是重写的,如果父类或接口中不含该方法,则编译报错 
 
java.lang 
Deprecated表示方法已过时,不鼓励使用,因为方法是危险的或者有其它更好的方法选择,在程序中使用该方法,会报编译警告 
 
java.lang 
SuppressWarnings告诉编译器忽略掉注解中声明的警告 
 
java.lang.annotation 
Documented表示可以包含在javadoc等工具生成的文档中 
 
java.lang.annotation 
Inherited表示注解类型会被继承到子类中 
 
java.lang.annotation 
Retention表示注解类型要保留多久,取值于枚举类 
java.lang.annotation.RetentionPolicy
 
java.lang.annotation 
Target表示注解适用那种Java成员,取值于枚举类 
java.lang.annotation.ElementType
 
java.lang.annotation 
Native表示一个字段定义常量值引用自native代码,1.8新增 
 
java.lang.annotation 
Repeatable表示注解可以声明多次的,1.8新增 
java.lang.annotation.RetentionPolicy:public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,
    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,
    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
java.lang.annotation.ElementType:public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,
    /** Field declaration (includes enum constants) */
    FIELD,
    /** Method declaration */
    METHOD,
    /** Formal parameter declaration */
    PARAMETER,
    /** Constructor declaration */
    CONSTRUCTOR,
    /** Local variable declaration */
    LOCAL_VARIABLE,
    /** Annotation type declaration */
    ANNOTATION_TYPE,
    /** Package declaration */
    PACKAGE,
    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,
    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}
18.2. 自定义注解
Table标识数据库中表,适用于TYPE类上,注解Column标识数据库中的列,适用于FIELD字段上@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Table {
    String value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
    String value();
}
javabean类,Student类和Book类
因为数据库中的字段与我们实体类中字段名字可能不一致,因此我们使用注解标识实体类与数据库中相关字段的对应关系@Table("student")
public class Student {
    @Column(value = "std_id")
    int id;
    @Column(value = "std_name")
    String name;
    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;
    }
}
@Table("book")
public class Book {
    @Column("book_name")
    String name;
    @Column("book_author")
    String author;
    @Column("price")
    int price;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
}
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLHelperBook book0 = new Book();
book0.setName("Thinking in java");
book0.setAuthor("Json");
book0.setPrice(80);
new SQLHelper四月 14, 2020 1:24:08 上午 SQLHelper insert0
信息: INSERT INTO book(book_name, book_author, price) VALUES("Thinking in java", "Json", 80);
四月 14, 2020 1:24:08 上午 SQLHelper insert0
信息: INSERT INTO student(std_id, std_name) VALUES(1637, "Tom");