注解:java 自定义注解应用实例

本例子旨在使用自定义注解为实体打上标记,为自动生成 sql 提供依据,模拟 hibernate 的注解,至于注解的原理自己搜吧

1.定义 Table 注解

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


2.定义 Column 注解

[java] view plain copy
 
  1. package test;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10. @Inherited  
  11. @Target({ElementType.FIELD})  
  12. @Retention(RetentionPolicy.RUNTIME)  
  13. @Documented  
  14. public @interface Column {  
  15.     String value() default "";  
  16. }  


3.定义使用注解的实体

[java] view plain copy
 
  1. package test;  
  2.   
  3.   
  4. @Table("tb_test")  
  5. public class TestDto {  
  6.       
  7.     @Deprecated  
  8.     private String tt;  
  9.       
  10.     @Column("_id")  
  11.     private String id;  
  12.       
  13.     @Column("username")  
  14.     private String name;  
  15.       
  16.     public TestDto(String id, String name) {  
  17.         super();  
  18.         this.id = id;  
  19.         this.name = name;  
  20.     }  
  21.   
  22.     public String getId() {  
  23.         return id;  
  24.     }  
  25.   
  26.     public void setId(String id) {  
  27.         this.id = id;  
  28.     }  
  29.   
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.       
  38.       
  39. }  


4.测试注解

[java] view plain copy
 
  1. package test;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.lang.reflect.Method;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) {  
  8.         TestDto testDto = new TestDto("123", "34");  
  9.         TestDto testDto1 = new TestDto("123", "test1");  
  10.         TestDto testDto2 = new TestDto("", "test1,test2,test3,test4");  
  11.         String sql = assembleSqlFromObj(testDto);  
  12.         String sql1 = assembleSqlFromObj(testDto1);  
  13.         String sql2 = assembleSqlFromObj(testDto2);  
  14.         System.out.println(sql);  
  15.         System.out.println(sql1);  
  16.         System.out.println(sql2);  
  17.     }  
  18.   
  19.     /** 
  20.      * 通过注解来组装查询条件,生成查询语句 
  21.      *  
  22.      * @param obj 
  23.      * @return 
  24.      */  
  25.     public static String assembleSqlFromObj(Object obj) {  
  26.         Table table = obj.getClass().getAnnotation(Table.class);  
  27.         StringBuffer sbSql = new StringBuffer();  
  28.         String tableName = table.value();  
  29.         sbSql.append("select * from " + tableName + " where 1=1 ");  
  30.         Field[] fileds = obj.getClass().getDeclaredFields();  
  31.         for (Field f : fileds) {  
  32.             String fieldName = f.getName();  
  33.             String methodName = "get" + fieldName.substring(0, 1).toUpperCase()  
  34.                     + fieldName.substring(1);  
  35.             try {  
  36.                 Column column = f.getAnnotation(Column.class);  
  37.                 if (column != null) {  
  38.                     Method method = obj.getClass().getMethod(methodName);  
  39.                     String value = (String) method.invoke(obj);  
  40.                     if (value != null && !value.equals("")) {  
  41.                         if (!isNum(column.value()) && !isNum(value)) {  
  42.                             // 判断参数是不是 in 类型参数 1,2,3  
  43.                             if (value.contains(",")) {  
  44.                                 sbSql.append(" and " + column.value() + " in (" + value + ") ");  
  45.                             } else {  
  46.                                 sbSql.append(" and " + column.value() + " like '%" + value + "%' ");  
  47.                             }  
  48.                         } else {  
  49.                             sbSql.append(" and " + column.value() + "=" + value + " ");  
  50.                         }  
  51.                     }  
  52.                 }  
  53.             } catch (Exception e) {  
  54.                 e.printStackTrace();  
  55.             }  
  56.         }  
  57.         return sbSql.toString();  
  58.     }  
  59.   
  60.     /** 
  61.      * 检查给定的值是不是 id 类型 1.检查字段名称 2.检查字段值 
  62.      *  
  63.      * @param target 
  64.      * @return 
  65.      */  
  66.     public static boolean isNum(String target) {  
  67.         boolean isNum = false;  
  68.         if (target.toLowerCase().contains("id")) {  
  69.             isNum = true;  
  70.         }  
  71.         if (target.matches("\d+")) {  
  72.             isNum = true;  
  73.         }  
  74.         return isNum;  
  75.     }  
  76. }  


测试结果:

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) 

原文地址:https://www.cnblogs.com/keyi/p/6797044.html