java 自定義註解應用例項
阿新 • • 發佈:2019-01-23
本例子旨在使用自定義註解為實體打上標記,為自動生成 sql 提供依據,模擬 hibernate 的註解,至於註解的原理自己搜吧
1.定義 Table 註解
package test; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Inherited @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Table { String value() default ""; }
2.定義 Column 註解
package test; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Inherited @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Column { String value() default ""; }
3.定義使用註解的實體
package test; @Table("tb_test") public class TestDto { @Deprecated private String tt; @Column("_id") private String id; @Column("username") private String name; public TestDto(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.測試註解
package test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
TestDto testDto = new TestDto("123", "34");
TestDto testDto1 = new TestDto("123", "test1");
TestDto testDto2 = new TestDto("", "test1,test2,test3,test4");
String sql = assembleSqlFromObj(testDto);
String sql1 = assembleSqlFromObj(testDto1);
String sql2 = assembleSqlFromObj(testDto2);
System.out.println(sql);
System.out.println(sql1);
System.out.println(sql2);
}
/**
* 通過註解來組裝查詢條件,生成查詢語句
*
* @param obj
* @return
*/
public static String assembleSqlFromObj(Object obj) {
Table table = obj.getClass().getAnnotation(Table.class);
StringBuffer sbSql = new StringBuffer();
String tableName = table.value();
sbSql.append("select * from " + tableName + " where 1=1 ");
Field[] fileds = obj.getClass().getDeclaredFields();
for (Field f : fileds) {
String fieldName = f.getName();
String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
Column column = f.getAnnotation(Column.class);
if (column != null) {
Method method = obj.getClass().getMethod(methodName);
String value = (String) method.invoke(obj);
if (value != null && !value.equals("")) {
if (!isNum(column.value()) && !isNum(value)) {
// 判斷引數是不是 in 型別引數 1,2,3
if (value.contains(",")) {
sbSql.append(" and " + column.value() + " in (" + value + ") ");
} else {
sbSql.append(" and " + column.value() + " like '%" + value + "%' ");
}
} else {
sbSql.append(" and " + column.value() + "=" + value + " ");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return sbSql.toString();
}
/**
* 檢查給定的值是不是 id 型別 1.檢查欄位名稱 2.檢查欄位值
*
* @param target
* @return
*/
public static boolean isNum(String target) {
boolean isNum = false;
if (target.toLowerCase().contains("id")) {
isNum = true;
}
if (target.matches("\\d+")) {
isNum = true;
}
return isNum;
}
}
測試結果:
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)