java-使用反射实现ORM映射

2020-12-13 01:53

阅读:426

标签:ret   lan   har   imp   rgs   interface   rom   string   注解   

自定义两个注解

 1 package com.moon.ROM;
 2 
 3 import java.lang.annotation.ElementType;
 4 import java.lang.annotation.Retention;
 5 import java.lang.annotation.RetentionPolicy;
 6 import java.lang.annotation.Target;
 7 
 8 @Target(ElementType.FIELD)
 9 @Retention(RetentionPolicy.RUNTIME)
10 public @interface SexFileAnnotation {
11     String name();
12     String type();
13     int length();
14 
15 }
 1 package com.moon.ROM;
 2 
 3 import java.lang.annotation.ElementType;
 4 import java.lang.annotation.Retention;
 5 import java.lang.annotation.RetentionPolicy;
 6 import java.lang.annotation.Target;
 7 
 8 @Target(ElementType.TYPE)
 9 @Retention(RetentionPolicy.RUNTIME)
10 public @interface SexTable {
11     String value();
12 
13 }
 1 package com.moon.ROM;
 2 @SexTable("tb_student")
 3 public class SxtStudent {
 4     @SexFileAnnotation(name ="studentName" ,type = "vachar",length = 10)
 5     private String studentName;
 6     @SexFileAnnotation(name="age",type = "int",length = 3)
 7     private int age;
 8     @SexFileAnnotation(name = "id",type = "int",length = 5)
 9     private int id;
10 
11     public String getStudentName() {
12         return studentName;
13     }
14 
15     public void setStudentName(String studentName) {
16         this.studentName = studentName;
17     }
18 
19     public int getAge() {
20         return age;
21     }
22 
23     public void setAge(int age) {
24         this.age = age;
25     }
26 
27     public int getId() {
28         return id;
29     }
30 
31     public void setId(int id) {
32         this.id = id;
33     }
34 }
 1 package com.moon.ROM;
 2 
 3 import java.lang.reflect.Field;
 4 
 5 public class Test {
 6     public static void main(String[] args) {
 7         try {
 8             Class clazz = Class.forName("com.moon.ROM.SxtStudent");
 9 //            Annotation[] annotations = clazz.getAnnotations();
10            /* for (Annotation annotation : annotations) {
11                 System.out.println(annotation);
12 
13             }*/
14            SexTable st = (SexTable) clazz.getAnnotation(SexTable.class);
15             Field f = clazz.getDeclaredField("studentName");
16             SexFileAnnotation sexf = f.getAnnotation(SexFileAnnotation.class);
17             System.out.println(sexf.name()+"---"+sexf.type()+"---"+sexf.length());
18 
19 
20         } catch (Exception e) {
21             e.printStackTrace();
22         }
23 
24     }
25 }

 

java-使用反射实现ORM映射

标签:ret   lan   har   imp   rgs   interface   rom   string   注解   

原文地址:https://www.cnblogs.com/xiaoqiqistudy/p/11017388.html


评论


亲,登录后才可以留言!