Annotation注解初识
注解本质上就是一个接口,该接口默认继承Annotation接口
-
元注解
元注解的作用就是描述其他注解。Java1.5定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java1.5定义的元注解有以下几种:
- @Target
- @Retention
- @Documented
- @Inherited
@Target
用于描述注解的范围,即注解在哪用。它说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)等。取值类型(ElementType)有以下几种:
- CONSTRUCTOR:用于描述构造器
- FIELD:用于描述域即类成员变量
- LOCAL_VARIABLE:用于描述局部变量
- METHOD:用于描述方法
- PACKAGE:用于描述包
- PARAMETER:用于描述参数
- TYPE:用于描述类、接口(包括注解类型) 或enum声明
- TYPE_PARAMETER:1.8版本开始,描述类、接口或enum参数的声明
- TYPE_USE:1.8版本开始,描述一种类、接口或enum的使用声明
@Retention
用于描述注解的生命周期,表示需要在什么级别保存该注解,即保留的时间长短。取值类型(RetentionPolicy)有以下几种:
- SOURCE:在源文件中有效(即源文件保留)
- CLASS:在class文件中有效(即class保留)
- RUNTIME:在运行时有效(即运行时保留)
@Documented
用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。它是一个标记注解,没有成员。
@Inherited
用于表示某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
-
通过注解实现生成对应的SQL语句
@Target({ElementType.FIELD})//表示作用域是字段
@Retention(RetentionPolicy.RUNTIME)//生命周期为运行时
public @interface Column {
String value();
}
@Table("user")
public class Filer {
@Column("id")
private int id;
@Column("userName")
private String userName;
@Column("nickName")
private String nickName;
@Column("age")
private int age;
@Column("city")
private String city;
@Column("email")
private String email;
//省略getter,setter
}
/* * 通过注释得到sql语句 */ public class Test { public static String query(Object f) { StringBuilder sb = new StringBuilder(); // 获取到f的class对象 Class<?> c = f.getClass(); // 判断是否存在注释 boolean tableExists = c.isAnnotationPresent(Table.class); if (!tableExists) { return null; } // 获取Table 注释中的数据 Table table = (Table) c.getAnnotation(Table.class); String tableValue = table.value(); sb.append("select * from " + tableValue + " where 1=1");// 初始化sql语句防止报错 // 遍历所有的字段 //由于字段的修饰符为private,使用getDeclaredFields()获取 Field[] fields = c.getDeclaredFields(); for (Field field : fields) { boolean columnExists = field.isAnnotationPresent(Column.class); if (!columnExists) { continue; } // 获取Column 注释中的数据(字段) Column column = field.getAnnotation(Column.class); String columnValue = column.value(); // 获取的方法名字 String getMethodName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1); // 通过反射调用get方法(获取字段值) Object fieldValue = null; try { Method getMethod = c.getMethod(getMethodName); fieldValue = getMethod.invoke(f); } catch (Exception e) { e.printStackTrace(); } // 拼装sql if (fieldValue instanceof String && (String) fieldValue != null || fieldValue instanceof Integer && (Integer) fieldValue != 0) { if (fieldValue instanceof String) { if (((String) fieldValue).contains(",")) { String[] values = ((String) fieldValue).split(","); sb.append(" in("); for (String value : values) { sb.append("'").append(value).append("',"); } sb.deleteCharAt(sb.length() - 1); sb.append(")"); } else { sb.append(" and ").append(columnValue + "='" + fieldValue + "'"); } } else if (fieldValue instanceof Integer) { sb.append(" and ").append(columnValue + "=" + fieldValue); } } } return sb.toString(); } public static void main(String[] args) { Filer f1 = new Filer(); f1.setId(10);// 查询id为10的用户 f1.setAge(16); Filer f2 = new Filer(); f2.setUserName("lucy");// 查询用户为lucy的用户 Filer f3 = new Filer(); f3.setEmail("liu@qq.com,zh@163.com,7777@sina.com");// 查询email为其中任何一个 String sql1 = query(f1); String sql2 = query(f2); String sql3 = query(f3); System.out.println(sql1); System.out.println(sql2); System.out.println(sql3); } } /*select * from user where 1=1 and id=10 and age=16*/ /*select * from user where 1=1 and userName='lucy'*/ /*select * from user where 1=1 in('liu@qq.com','zh@163.com','7777@sina.com')*/

浙公网安备 33010602011771号