Unit 1 : 注解
Topic One:什麼是注解

注意:注釋是comment;
Topic Two:幾種内置注解
這個方法的上面就有這個 他的意思是不建議使用;
注意:直接使用all可以壓制全部,你也可以對應的研制;
代碼如下:
package Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
*
* 測試一下Annotation的作用
*
* @author Wang
*
*/
public class Demo01 {
@Override //重寫
public String toString(){
return "";
}
@Deprecated //不建議使用的方法
public static int add(int a ,int b){
return a+b;
}
@SuppressWarnings("all")//壓制警告的作用
public static void test(){
List L1 = new ArrayList();
}
@SuppressWarnings("all")
public static void main(String[] args){
Date a = new Date();
System.out.println(add(3,4));//我們可以看見我們在使用不建議使用的方法的時候 方法的上面會有一個橫線;
}
}
Topic Three:自定義注解
我們在定義注解的時候需要先定義元注解:
元注解的作用就是說明注解的使用範圍等等 的解釋;
Target:來描述注解的使用範圍:
元注解Retention:
自己定義的一個注解:
package Test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* 自定義一個自己的注解
*
* @author wangtong
*
*/
@Target(value={ElementType.METHOD,ElementType.TYPE})
//這一行的作用是 規定這個注解可以用于方法和Type(Type的類,接口,枚舉等等)的前面
@Retention(RetentionPolicy.RUNTIME)
//這個元注解的作用就是使我們寫的那個注解 儲存為運作時有效
public @interface MyAnnotation {
//我們可以給注解添加參數清單,但是如果我們在添加參數後如果不寫預設值的話
//那麼我們在用注解的時候就要添加上去
String studentName() default "";
int age() default 0;
int id() default -1; //String indexOf("abc") -1
// -1的作用在這裡是不存在
String[] schools() default {"清華大學","北京大學"};
}
測試自己寫的這個注解:
package Test;
/**
*
* 測試自己寫的注解
*
* @author wangtong
*
*/
@MyAnnotation
//可以修飾類 這裡沒有對注解的參數進行複制 那這裡使用的就是預設值
public class Demo02 {
//可以修飾方法 這裡我們自己就給參數就行指派了
@MyAnnotation(age=19,studentName="啊哈",id=1,
schools={"北京大學","南陽理工"})
public void test(){
}
}
Unit 2: 反射機制讀取注解
在這裡我們可以看出注解的真正的作用;
我們通過注解可以把對應的Sql的語句跟寫進去;
然後我們根據反射來把注解給讀出來這樣我們我可以得到Sql語句進而得到那個表格;
說白了就我們Java和資料庫的結合;
Topic 1:我們來完成尚學堂的這個作業
step 1:先寫注解
我們可以明顯的看出我們應該寫一個Table的注解 來存放表的名字
然後我們應該寫一個屬性的注解
代碼如下:
package Test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* 表的注解,這裡隻有一個表的名字
*
* @author Wang
*
*/
@Target(value={ElementType.METHOD,ElementType.TYPE})
//這一行的作用是 規定這個注解可以用于方法和Type(Type的類,接口,枚舉等等)的前面
@Retention(RetentionPolicy.RUNTIME)
//這個元注解的作用就是使我們寫的那個注解 儲存為運作時有效
public @interface Table {
String value();
//當注解隻有一個參數的時候我們把這個參數命名為value
}
package Test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* 屬性的注解
*
* @author wangtong
*
*/
@Target(value={ElementType.METHOD,ElementType.TYPE})
//這一行的作用是 規定這個注解可以用于方法和Type(Type的類,接口,枚舉等等)的前面
@Retention(RetentionPolicy.RUNTIME)
//這個元注解的作用就是使我們寫的那個注解 儲存為運作時有效
public @interface Filed {
String columnName();//列名
String type(); //類型
int length(); //長度
}
step 2 : 我們利用類來存放表的資訊,利用注解來存放來轉化為資料庫語言的資訊;
package Test;
/**
*
* student類來存放資訊;
* 注解來存放資料庫語言的資訊
*
* @author wangtong
*
*/
@Table("tb_student")
public class Student {
@Filed(columnName = "id" , type = "int" , length = 10)
private int id;
//這裡我們帶上注解 就可以很容易的利用反射來讀取注解的内容 便于轉化為資料庫語言
@Filed(columnName = "sname" , type = "varchar" , length = 10)
private String sname;
@Filed(columnName = "age" , type = "int" , length = 3)
private int age;
public Student(int id, String sname, int age) {
super();
this.id = id;
this.sname = sname;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
step3 : 利用反射來把注解裡面的内容給轉化成資訊(用于資料庫的編寫)
package Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
*
* 我們利用反射來讀取資訊
*
* @author Wang
*
*/
public class Demo03 {
public static void main(String[] args){
try {
//我們這裡讀取student的資訊 注意這裡關聯的時候 隻要包名和類名就好了
Class classInfo = Class.forName("Test.Student");
//獲得類的所有有效注解
Annotation[] annotations=classInfo.getAnnotations();
//用增強for 周遊列印
for (Annotation a : annotations)
System.out.println(a);
//獲得類的指定的注解
//關聯了Table這個注解類
Table t = (Table) classInfo.getAnnotation(Table.class);
System.out.println(t.value());
//獲得類的屬性的注解
Field f = classInfo.getDeclaredField("sname");
MyField Field1 = f.getAnnotation(MyField.class);
System.out.println(Field1.columnName()+"--"+Field1.type()+"--"+Field1.length());
//根據獲得的表名、字段的資訊,拼出DDL語句,然後,使用JDBC執行這個SQL,
//在資料庫中生成相關的表
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}