ORM框架注解开发原理

@Target(value = { ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

public @interface SetTable {

    String value();

}


@Retention(RetentionPolicy.RUNTIME)

public @interface SetProperty {

 

      String  name();

   

      int leng();

}



@Table(value = "it_user")

class User {

 

      @ProPerty(value = "it_id", leng = 10)

      private String id;

      @ProPerty(value = "it_name", leng = 10)

      private String name;

      public String getId() {

           return id;

      }

      public void setId(String id) {

           this.id = id;

      }

      public String getName() {

           return name;

      }

      public void setName(String name) {

           this.name = name;

      }

}

 

@Target(value = ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@interface Table {

      String value();

}

@Retention(RetentionPolicy.RUNTIME)

@interface ProPerty {

      String value();

      int leng();

}

public class Test002 {

      public static void main(String[] args) throws ClassNotFoundException {

           ClassforName= Class.forName("com.itmayiedu.User");

           StringBuffersf = new StringBuffer();

           sf.append(" select ");

           // 获取当前的所有的属性

           Field[]declaredFields = forName.getDeclaredFields();

           for (int i = 0; i < declaredFields.length; i++) {

                 Fieldfield = declaredFields[i];

                 ProPerty proPertyAnnota = field.getDeclaredAnnotation(ProPerty.class);

                 StringproPertyName = proPertyAnnota.value();

                 sf.append("

  " + proPertyName);

                 if (i < declaredFields.length - 1) {

                      sf.append(" ,

  ");

                 }

           }

           Table tableAnnota = forName.getDeclaredAnnotation(Table.class);

           // 表的名称

           StringtableName = tableAnnota.value();

           sf.append(" from " + tableName);

           System.out.println(sf.toString());

      }

}

你可能感兴趣的:(ORM框架注解开发原理)