日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java annotation(注解)

 噢麥噶 2012-04-01
通過一個(gè)例子來認(rèn)識(shí)注解:由javaBean注解生成建表sql 

定義表名注解 
Java代碼  收藏代碼
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5. /*注解的分類 
  6.  * 1.標(biāo)記注解(marker annotation) 
  7.  * 注解體內(nèi)沒有定義任何元素,只起一個(gè)標(biāo)記提示作用 
  8.  * 常見的就是java.lang包下的Deprecated,Override,SuppressWarnings 
  9.    Deprecated 編譯時(shí)會(huì)提示方法過時(shí) 
  10.    Override 編譯時(shí)驗(yàn)證重寫父類方法簽名是否正確 
  11.    SuppressWarnings 取消警告 
  12.    2.元注解 
  13.    只用來修飾注解定義的注解 
  14.    下面用到的Retention,Target 
  15.    Retention用來指定定義的注解要保留到什么時(shí)候 
  16.    有三個(gè)枚舉值: 
  17.    RetentionPolicy.SOURCE 編譯是會(huì)調(diào)用,不會(huì)保留到class文件中 
  18.    RetentionPolicy.CLASS  會(huì)跟隨保留到class文件中 
  19.    RetentionPolicy.RUNTIME 保留到class文件中,并且class被加載時(shí)還可以通過反射操作注解 
  20.     
  21.    Target用來規(guī)定注解可以修飾的程序元素的種類 
  22.    其有一個(gè)ElementType[]的枚舉數(shù)組參數(shù) 
  23.     ElementType.PACKAGE 包  
  24.     ElementType.TYPE 類,接口,注解,枚舉 
  25.     ElementType.METHOD 方法聲明 
  26.     ElementType.FIELD  字段 
  27.     ...... 
  28.  * 注解一旦定義好之后,就可以像使用public,static這樣的的modifiers一樣,用注解修飾類,方法或?qū)傩?/span> 
  29.  */  
  30. @Retention(RetentionPolicy.RUNTIME)//可以保留到類被加載運(yùn)行時(shí)  
  31. @Target(ElementType.TYPE) //指定該注解用來修飾類...  
  32. public @interface Table { //定義注解的關(guān)鍵字@interface  
  33.     /* 
  34.      * 元素定義的返回類型限定為:基本類型,String,Class,emum,annotation 
  35.         或者是前述類型的數(shù)組 
  36.      */  
  37.       
  38.     String name();  
  39. }  


定義字段注解 

Java代碼  收藏代碼
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5. /* 
  6.  * 定義字段的注解 
  7.  */  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Target(ElementType.FIELD) //該注解只能用在成員變量上  
  10. public @interface Column {  
  11.     int length() default 0//用來存放字段的長(zhǎng)度  
  12.     String name() default "" ;//用來存放字段的名字  
  13.     //至于數(shù)據(jù)庫字段的類型,后面根據(jù)反射獲取成員變量的類型獲取  
  14. }  


定義普通javaBean,用上面的注解界定建表sql 
Java代碼  收藏代碼
  1. import java.util.Date;  
  2. /* 
  3.  * 一個(gè)簡(jiǎn)單使用例子,根據(jù)注解生成創(chuàng)建表語句 
  4.  * 使用注解時(shí),可以用key-value鍵值對(duì)的形式為注解的元素賦值 
  5.  */  
  6. @Table(name="table_person"//表名  
  7. public class PersonBean {  
  8.     @Column(length=8,name="person_id")  
  9.     private Integer id;  
  10.     @Column(length=32,name="pname")  
  11.     private String name;  
  12.     @Column(name="birth"//Date類型不需要指定length  
  13.     private Date birth;  
  14.     public Integer getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.     public Date getBirth() {  
  27.         return birth;  
  28.     }  
  29.     public void setBirth(Date birth) {  
  30.         this.birth = birth;  
  31.     }  
  32. }  


解析注解生成建表sql 
Java代碼  收藏代碼
  1. import java.lang.annotation.Annotation;  
  2. import java.lang.reflect.Field;  
  3. import java.lang.reflect.Method;  
  4. import java.util.Date;  
  5.   
  6. public class TestMain {  
  7.     //用來解析所有成員變量的方法  
  8.     public static String[] getColumns(Field[] fArr){  
  9.         String[] columns = new String[fArr.length];  
  10.         String columnName="";  
  11.         int columnLength=0;  
  12.         String columnType = "";  
  13.         for(int i=0;i<fArr.length;i++){  
  14.             Field f = fArr[i];  
  15.             String name = f.getName(); //成員變量名  
  16.             Class type = f.getType(); //成員變量類型  
  17.             //判斷該成員變量上是不是存在Column類型的注解  
  18.             if(f.isAnnotationPresent(Column.class)){  
  19.                 //存在  
  20.                 Column c = f.getAnnotation(Column.class);//獲取實(shí)例  
  21.                 //獲取元素值  
  22.                 columnName = c.name();  
  23.                 columnLength = c.length();  
  24.             }  
  25.             //如果未指定列名,默認(rèn)列名使用成員變量名  
  26.             if("".equals(columnName)){  
  27.                 columnName = name;  
  28.             }  
  29.             //如果未指定字段長(zhǎng)度,默認(rèn)32  
  30.             if(0 == columnLength){  
  31.                 columnLength = 32;  
  32.             }  
  33.             //如果成員變量是String類型的,數(shù)據(jù)庫字段是varchar類型  
  34.             if(String.class == type){  
  35.                 columnType = "varchar";  
  36.             }else if(Integer.class == type){  
  37.                 columnType = "number";//Integer類型的用number  
  38.             }  
  39.             //把每一個(gè)成員變量相關(guān)信息存放到返回?cái)?shù)組中  
  40.             if(Date.class == type){//Date類型的用date  
  41.                 columns[i] = columnName+" date";  
  42.             }else{  
  43.                 columns[i] =  columnName+" "+columnType+"("+columnLength+")";  
  44.             }  
  45.         }  
  46.         return columns;  
  47.     }  
  48.     public static void main(String[] args) throws Exception {  
  49.       
  50.         StringBuffer sql = new StringBuffer("create table ");  
  51.         Class c1 = Class.forName("com.cao.annotation.PersonBean");//加載使用注解的bean,(bean的路徑包括bean的包)  
  52.         if(c1.isAnnotationPresent(Table.class)){  
  53.             //該class存在Table類型的注解,獲取指定的表名  
  54.             Table table = (Table) c1.getAnnotation(Table.class);  
  55.             String tableName = table.name();  
  56.             sql.append(tableName+" (");  
  57.         }  
  58.         //獲取bean所聲明的成員變量(include private)   
  59.         Field[] fArr = c1.getDeclaredFields();  
  60.         //解析這些字段的注解設(shè)定值  
  61.         String[] columns = getColumns(fArr);  
  62.         //拼接解析后的成員變量信息成創(chuàng)建表語句  
  63.         for(int i=0;i<columns.length;i++){  
  64.             if(i==(columns.length-1)){  
  65.                 sql.append("\n"+columns[i]+")");  
  66.             }else{  
  67.                 sql.append("\n"+columns[i]+",");  
  68.             }  
  69.         }  
  70.         System.out.println(sql.toString());  
  71.         /*結(jié)果: 
  72.          *  create table table_person ( 
  73.             person_id number(8), 
  74.             pname varchar(32), 
  75.             birth date) 
  76.          */  
  77.     }  
  78. }  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多