1. 程式人生 > >Java註解(Annotation)——3 例項

Java註解(Annotation)——3 例項

本例子旨在使用自定義註解為實體打上標記,為自動生成 sql 提供依據,模擬 hibernate 的註解,至於註解的原理自己搜吧

1.定義 Table 註解

  1. package test;  
  2. import java.lang.annotation.Documented;  
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Inherited;  
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.RetentionPolicy;  
  7. import java.lang.annotation.Target;  
  8. @Inherited
  9. @Target({ElementType.TYPE})  
  10. @Retention(RetentionPolicy.RUNTIME)  
  11. @Documented
  12. public@interface Table {  
  13.     String value() default"";  
  14. }  

2.定義 Column 註解
  1. package test;  
  2. import java.lang.annotation.Documented;  
  3. import java.lang.annotation.ElementType;  
  4. import
     java.lang.annotation.Inherited;  
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.RetentionPolicy;  
  7. import java.lang.annotation.Target;  
  8. @Inherited
  9. @Target({ElementType.FIELD})  
  10. @Retention(RetentionPolicy.RUNTIME)  
  11. @Documented
  12. public@interface Column {  
  13.     String value() default""
    ;  
  14. }  

3.定義使用註解的實體
  1. package test;  
  2. @Table("tb_test")  
  3. publicclass TestDto {  
  4.     @Deprecated
  5.     private String tt;  
  6.     @Column("_id")  
  7.     private String id;  
  8.     @Column("username")  
  9.     private String name;  
  10.     public TestDto(String id, String name) {  
  11.         super();  
  12.         this.id = id;  
  13.         this.name = name;  
  14.     }  
  15.     public String getId() {  
  16.         return id;  
  17.     }  
  18.     publicvoid setId(String id) {  
  19.         this.id = id;  
  20.     }  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     publicvoid setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27. }  

4.測試註解
  1. package test;  
  2. import java.lang.reflect.Field;  
  3. import java.lang.reflect.Method;  
  4. publicclass Test {  
  5.     publicstaticvoid main(String[] args) {  
  6.         TestDto testDto = new TestDto("123""34");  
  7.         TestDto testDto1 = new TestDto("123""test1");  
  8.         TestDto testDto2 = new TestDto("""test1,test2,test3,test4");  
  9.         String sql = assembleSqlFromObj(testDto);  
  10.         String sql1 = assembleSqlFromObj(testDto1);  
  11.         String sql2 = assembleSqlFromObj(testDto2);  
  12.         System.out.println(sql);  
  13.         System.out.println(sql1);  
  14.         System.out.println(sql2);  
  15.     }  
  16.     /** 
  17.      * 通過註解來組裝查詢條件,生成查詢語句 
  18.      *  
  19.      * @param obj 
  20.      * @return 
  21.      */
  22.     publicstatic String assembleSqlFromObj(Object obj) {  
  23.         Table table = obj.getClass().getAnnotation(Table.class);  
  24.         StringBuffer sbSql = new StringBuffer();  
  25.         String tableName = table.value();  
  26.         sbSql.append("select * from " + tableName + " where 1=1 ");  
  27.         Field[] fileds = obj.getClass().getDeclaredFields();  
  28.         for (Field f : fileds) {  
  29.             String fieldName = f.getName();  
  30.             String methodName = "get" + fieldName.substring(01).toUpperCase()  
  31.                     + fieldName.substring(1);  
  32.             try {  
  33.                 Column column = f.getAnnotation(Column.class);  
  34.                 if (column != null) {  
  35.                     Method method = obj.getClass().getMethod(methodName);  
  36.                     String value = (String) method.invoke(obj);  
  37.                     if (value != null && !value.equals("")) {  
  38.                         if (!isNum(column.value()) && !isNum(value)) {  
  39.                             // 判斷引數是不是 in 型別引數 1,2,3
  40.                             if (value.contains(",")) {  
  41.                                 sbSql.append(" and " + column.value() + " in (" + value + ") ");  
  42.                             } else {  
  43.                                 sbSql.append(" and " + column.value() + " like '%" + value + "%' ");  
  44.                             }  
  45.                         } else {  
  46.                             sbSql.append(" and " + column.value() + "=" + value + " ");  
  47.                         }  
  48.                     }  
  49.                 }  
  50.             } catch (Exception e) {  
  51.                 e.printStackTrace();  
  52.             }  
  53.         }  
  54.         return sbSql.toString();  
  55.     }  
  56.     /** 
  57.      * 檢查給定的值是不是 id 型別 1.檢查欄位名稱 2.檢查欄位值 
  58.      *  
  59.      * @param target 
  60.      * @return 
  61.      */
  62.     publicstaticboolean isNum(String target) {  
  63.         boolean isNum = false;  
  64.         if (target.toLowerCase().contains("id")) {  
  65.             isNum = true;  
  66.         }  
  67.         if (target.matches("\\d+")) {  
  68.             isNum = true;  
  69.         }  
  70.         return isNum;  
  71.     }  
  72. }  

測試結果:

select * from tb_test where 1=1  and _id=123  and username=34 
select * from tb_test where 1=1  and _id=123  and username like '%test1%' 
select * from tb_test where 1=1  and username in (test1,test2,test3,test4) 


原文地址:http://blog.csdn.net/tengdazhang770960436/article/details/37886361