分享

java annotation(注解)

 噢麦噶 2012-04-01
通过一个例子来认识注解:由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.标记注解(marker annotation) 
  7.  * 注解体内没有定义任何元素,只起一个标记提示作用 
  8.  * 常见的就是java.lang包下的Deprecated,Override,SuppressWarnings 
  9.    Deprecated 编译时会提示方法过时 
  10.    Override 编译时验证重写父类方法签名是否正确 
  11.    SuppressWarnings 取消警告 
  12.    2.元注解 
  13.    只用来修饰注解定义的注解 
  14.    下面用到的Retention,Target 
  15.    Retention用来指定定义的注解要保留到什么时候 
  16.    有三个枚举值: 
  17.    RetentionPolicy.SOURCE 编译是会调用,不会保留到class文件中 
  18.    RetentionPolicy.CLASS  会跟随保留到class文件中 
  19.    RetentionPolicy.RUNTIME 保留到class文件中,并且class被加载时还可以通过反射操作注解 
  20.     
  21.    Target用来规定注解可以修饰的程序元素的种类 
  22.    其有一个ElementType[]的枚举数组参数 
  23.     ElementType.PACKAGE 包  
  24.     ElementType.TYPE 类,接口,注解,枚举 
  25.     ElementType.METHOD 方法声明 
  26.     ElementType.FIELD  字段 
  27.     ...... 
  28.  * 注解一旦定义好之后,就可以像使用public,static这样的的modifiers一样,用注解修饰类,方法或属性 
  29.  */  
  30. @Retention(RetentionPolicy.RUNTIME)//可以保留到类被加载运行时  
  31. @Target(ElementType.TYPE) //指定该注解用来修饰类...  
  32. public @interface Table { //定义注解的关键字@interface  
  33.     /* 
  34.      * 元素定义的返回类型限定为:基本类型,String,Class,emum,annotation 
  35.         或者是前述类型的数组 
  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//用来存放字段的长度  
  12.     String name() default "" ;//用来存放字段的名字  
  13.     //至于数据库字段的类型,后面根据反射获取成员变量的类型获取  
  14. }  


定义普通javaBean,用上面的注解界定建表sql 
Java代码  收藏代码
  1. import java.util.Date;  
  2. /* 
  3.  * 一个简单使用例子,根据注解生成创建表语句 
  4.  * 使用注解时,可以用key-value键值对的形式为注解的元素赋值 
  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);//获取实例  
  21.                 //获取元素值  
  22.                 columnName = c.name();  
  23.                 columnLength = c.length();  
  24.             }  
  25.             //如果未指定列名,默认列名使用成员变量名  
  26.             if("".equals(columnName)){  
  27.                 columnName = name;  
  28.             }  
  29.             //如果未指定字段长度,默认32  
  30.             if(0 == columnLength){  
  31.                 columnLength = 32;  
  32.             }  
  33.             //如果成员变量是String类型的,数据库字段是varchar类型  
  34.             if(String.class == type){  
  35.                 columnType = "varchar";  
  36.             }else if(Integer.class == type){  
  37.                 columnType = "number";//Integer类型的用number  
  38.             }  
  39.             //把每一个成员变量相关信息存放到返回数组中  
  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.         //解析这些字段的注解设定值  
  61.         String[] columns = getColumns(fArr);  
  62.         //拼接解析后的成员变量信息成创建表语句  
  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.         /*结果: 
  72.          *  create table table_person ( 
  73.             person_id number(8), 
  74.             pname varchar(32), 
  75.             birth date) 
  76.          */  
  77.     }  
  78. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多