@guoxs
2016-06-04T14:23:07.000000Z
字数 3272
阅读 2496
java
注解: Java提供了一种源程序中的元素关联任何信息和任何元数据的途径和方法。
JDK自带注解
: 编译时注解
@Override
@Deprecated
@Suppvisewarnings
常见第三方注解
Spring
Mybatis
注解的分类
按运行机制分:
按来源分:
自定义注解
语法要求:
//元注解
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited //允许子继承
@Documented //生成javadoc时会包含注解
//使用@interface关键字定义注解
public @interface Description{
//成员以无参数无异常方式声明
String desc();
String author();
//可以用default为成员指定一个默认值
int age() default 18;
}
元注解
: 注解的注解
使用自定义注解
使用注解的语法:
@<注解名>(<成员名1>=<成员值1>, <成员名1>=<成员值1>, ...)
@Description(desc="I an eyeColor", author="Mooc boy", age=18)
public String eyeColor(){
return "red";
}
通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。
源码:
@Table("student")
public class Filter {
@Column("id")
private int stuId;
@Column("name")
private String stuName;
@Column("email")
private String email;
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Filter f1 = new Filter();
f1.setStuId(10);
Filter f2 = new Filter();
f2.setStuId(10);
f2.setStuName("baoyou");
Filter f3 = new Filter();
f3.setEmail("1223716098@qq.com,curiousby@163.com");
String sql1 = query(f1);
String sql2 = query(f2);
String sql3 = query(f3);
System.out.println(sql1);
System.out.println(sql2);
System.out.println(sql3);
}
@SuppressWarnings("unchecked")
private static String query(Filter f) {
StringBuffer sb = new StringBuffer();
Class c= f.getClass();
boolean isExist = c.isAnnotationPresent(Table.class);
if (!isExist) {
return null;
}
Table t = (Table) c.getAnnotation(Table.class);
String tableName = t.value();
sb.append(" select * from ").append(tableName).append(" 1=1 ");
Field[] fArray = c.getDeclaredFields();
for (Field field : fArray) {
boolean fExist = field.isAnnotationPresent( Column.class);
if (!fExist) {
continue;
}
Column column = field.getAnnotation(Column.class);
String columnName = column.value();
String fieldName = field.getName();
String getMethodName = "get" +fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
Object fieldValue=null;
try {
Method getMethod =c.getMethod(getMethodName);
fieldValue = getMethod.invoke(f);
} catch (Exception e) {
}
if (fieldValue == null || (fieldValue instanceof Integer && (Integer)fieldValue == 0) ) {
continue;
}
sb.append(" and ").append( columnName );
if (fieldValue instanceof String){
if (((String) fieldValue ).contains(",")) {
String[] values = ((String) fieldValue ).split(",");
sb.append(" in ( ");
for (String v : values) {
sb.append("'").append(v).append("',");
}
sb.deleteCharAt(sb.length()-1);
sb.append(" )");
} else{
sb.append(" = '").append(fieldValue).append("' ");
}
}else if (fieldValue instanceof Integer){
sb.append(" = ").append(fieldValue).append(" ");
}
}
return sb.toString();
}
}