• java-使用反射实现ORM映射


    自定义两个注解

     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 }
  • 相关阅读:
    NPOI单元格公式不刷新
    DIV+CSS HACK
    简答好用的邮件服务器hMailServer(转)
    C# 后台POST和GET 获取数据
    Quartz.Net1.0.2.3 配置记录
    ASP.NET自定义控件VS2012中添加失败(下列控件已成功添加到工具箱中,但未在活动设计器中启用)
    NPOI 1.2.5复制行(包括格式)
    Javascript中Null和Undefined的区别[转]
    测试流程(立项会)
    测试计划
  • 原文地址:https://www.cnblogs.com/xiaoqiqistudy/p/11017388.html
Copyright © 2020-2023  润新知