注解Demo
分类:
IT文章
•
2024-01-02 12:18:48
1 //Java中提供了四种元注解,专门负责注解其他的注解,分别如下
2 16
3 17 //@Retention元注解,表示需要在什么级别保存该注释信息(生命周期)。可选的RetentionPoicy参数包括:
4 18 //RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉
5 19 //RetentionPolicy.CLASS:停留在class文件中,但会被VM丢弃(默认)
6 20 //RetentionPolicy.RUNTIME:内存中的字节码,VM将在运行时也保留注解,因此可以通过反射机制读取注解的信息
7 21
8 22 //@Target元注解,默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括
9 23 //ElementType.CONSTRUCTOR: 构造器声明
10 24 //ElementType.FIELD: 成员变量、对象、属性(包括enum实例)
11 25 //ElementType.LOCAL_VARIABLE: 局部变量声明
12 26 //ElementType.METHOD: 方法声明
13 27 //ElementType.PACKAGE: 包声明
14 28 //ElementType.PARAMETER: 参数声明
15 29 //ElementType.TYPE: 类、接口(包括注解类型)或enum声明
16 30
17 31 //@Documented将注解包含在JavaDoc中
18 32
19 33 //@Inheried允许子类继承父类中的注解
1 @Inherited
2 @Documented
3 @Retention(RetentionPolicy.RUNTIME)
4 @Target(ElementType.TYPE)
5 public @interface TableName {
6 String value();
7 }
View Code
1 @Documented
2 @Inherited
3 @Retention(RetentionPolicy.RUNTIME)
4 @Target(ElementType.FIELD)
5 public @interface FieldProperty {
6 String value();
7 boolean quote() default true;
8 }
View Code
1 public interface Model {
2
3 }
View Code
1 @TableName("T_USER")
2 public class User implements Model{
3 @FieldProperty("ID")
4 private String id;
5 @FieldProperty("NAME")
6 private String name;
7 public final String getId() {
8 return id;
9 }
10 public final void setId(String id) {
11 this.id = id;
12 }
13 public final String getName() {
14 return name;
15 }
16 public final void setName(String name) {
17 this.name = name;
18 }
19
20 }
View Code
1 public class SQLUtil {
2 public static void main(String[] args) {
3 User user = new User();
4 user.setId("001");
5 user.setName("wzq");
6 insert(user);
7 }
8
9 @SuppressWarnings("unchecked")
10 public static <T extends Model> void insert(T t) {
11 Class<T> clz = (Class<T>) t.getClass();
12 if (clz.isAnnotationPresent(TableName.class)) {
13 // 表名
14 String tableName = clz.getAnnotation(TableName.class).value();
15 // 字段名
16 Field[] flds = clz.getDeclaredFields();
17 String[] fields = new String[flds.length];
18 // 字段值
19 String[] values = new String[flds.length];
20 for (int i = 0; i < flds.length; i++) {
21 Field fld = flds[i];
22 fld.setAccessible(true);
23 // String fields[i] =
24 // fld.getAnnotation(FieldProperty.class).value();
25 fields[i] = fld.getAnnotation(FieldProperty.class).value();
26 try {
27 values[i] = (String) fld.get(t);
28 } catch (IllegalArgumentException e) {
29 e.printStackTrace();
30 } catch (IllegalAccessException e) {
31 e.printStackTrace();
32 }
33 }
34 String sql = "insert into " + tableName + "(" + join(fields, ',')
35 + ")values(" + join(values, ',') + ')';
36 System.out.println(sql);
37 }
38 }
39
40 public static final String join(Object[] arrs, char symbol) {
41 if (arrs == null) {
42 return "";
43 }
44 StringBuffer sb = new StringBuffer();
45 for (int i = 0; i < arrs.length; i++) {
46 Object obj = arrs[i];
47 sb.append(obj);
48 if (i != arrs.length - 1) {
49 sb.append(symbol);
50 }
51 }
52 return sb.toString();
53 }
54 }