一 元注解
@Target 表示 注解作用域 file (实例) method 方法 type 类
@Retention runTIme 在运行时可用 class 类文件中的使用,source 注解被编译器丢弃(源代码期间使用)
@Documented javadoc文件
@Inhertied 子类继承
二 自定义注解
简单点 就是自己编写个自定义注解,然后在通过反射 解析注解。java 中的万物皆对象,(Class.forName(),类也对象像,method方法也是对象)
@interface nn()
{
String value();
}
private static String select(demo d1) {
// TODO Auto-generated method stub
StringBuilder sb=new StringBuilder();
sb.append("select * from");
Class c=d1.getClass();
if(c.isAnnotationPresent(table.class)){
table tt = (table)c.getAnnotation(table.class);
sb.append(tt.value()).append("where 1=1");//加载 名字(table)
Field[] files = c.getDeclaredFields();
//获取所有字段
for (int i = 0; i < files.length; i++) {
if(files[i].isAnnotationPresent(coumnt.class)){
coumnt coumnt = files[i].getAnnotation(coumnt.class);
//获取字段 ming cheng
Object zhi = null;
String cc=coumnt.value();
String methodname="get"+files[i].getName().substring(0,1).toUpperCase()+files[i].getName().substring(1);
try {
//获取 字段值
Method method = c.getMethod(methodname);
zhi =method.invoke(d1,null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cc==null||(zhi instanceof Integer &&(Integer)zhi==0)){
continue;
}
sb.append("and").append(cc).append("=").append("'").append(zhi).append("'");
}
else{
System.err.println(" xoumnt not exist");
continue;
}
}
}
else{
System.out.println("table.class is not exist");
}
return sb.toString();
}
}