zoukankan      html  css  js  c++  java
  • 反射(操作注解)

    了解什么是ORM?

    Object relationship Mapping-->对象关系映射

     1)类和表结构对应

    2)属性和字段对应

    3)对象和记录对应(new出来一个对象在数据库中就是一列)

    要求:利用注解和反射完成类和表结构的映射关系。

      1 package reflection;
      2 
      3 
      4 import javafx.scene.chart.StackedBarChart;
      5 
      6 import java.lang.annotation.*;
      7 
      8 // 练习反射操作注解
      9 public class Test12 {
     10     // 假如数据库是一样的,我们可以通过注解去操纵这个表
     11     public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
     12 
     13         // 获得反射的类的对象
     14         Class c1 = Class.forName("reflection.Student2");
     15 
     16         // 通过反射获得注解
     17         Annotation[] annotations = c1.getAnnotations();
     18         for (Annotation annotation:annotations) {
     19             System.out.println(annotation);
     20 
     21         }
     22 
     23         // 获取注解指定的value的值
     24         Table table = (Table)c1.getAnnotation(Table.class);
     25         String value = table.value();
     26         System.out.println(value);
     27 
     28         // 获得类指定的注解
     29          java.lang.reflect.Field f= c1.getDeclaredField("name");
     30 
     31         Field annotation = f.getAnnotation(Field.class);
     32         System.out.println(annotation.columnName()); //db_name
     33         System.out.println(annotation.type());  // varchar
     34         System.out.println(annotation.length()); // 3
     35     }
     36     
     37 }
     38 
     39 
     40 // 模拟数据库
     41 @Table("db_student")
     42 class Student2 {
     43     @Field(columnName = "db_id",type = "int",length = 10)
     44     private int id;
     45     @Field(columnName = "db_age",type = "int",length = 10)
     46     private int age;
     47     @Field(columnName = "db_name",type = "varchar",length = 3)
     48     private String name;
     49 
     50     public Student2() {
     51     }
     52 
     53     public Student2(int id, int age, String name) {
     54         this.id = id;
     55         this.age = age;
     56         this.name = name;
     57     }
     58 
     59     public int getId() {
     60         return id;
     61     }
     62 
     63     public int getAge() {
     64         return age;
     65     }
     66 
     67     public String getName() {
     68         return name;
     69     }
     70 
     71     public void setId(int id) {
     72         this.id = id;
     73     }
     74 
     75     public void setAge(int age) {
     76         this.age = age;
     77     }
     78 
     79     public void setName(String name) {
     80         this.name = name;
     81     }
     82 
     83     @Override
     84     public String toString() {
     85         return "Student2{" +
     86                 "id=" + id +
     87                 ", age=" + age +
     88                 ", name='" + name + '\'' +
     89                 '}';
     90     }
     91 }
     92 
     93 
     94 
     95 // 类名的注解
     96 @Target(ElementType.TYPE)
     97 @Retention(RetentionPolicy.RUNTIME)
     98 @interface Table {
     99     String value();  // 代表注解需要一个参数
    100 }
    101 
    102 // 属性的注解
    103 @Target(ElementType.FIELD)
    104 @Retention(RetentionPolicy.RUNTIME)
    105 @interface Field{
    106     String columnName(); // 列名
    107     String type(); // 类型
    108     int length(); // 长度
    109 }
  • 相关阅读:
    python mysql Connect Pool mysql连接池 (201
    mysql: see all open connections to a given database?
    mysql int(3)与int(11)的区别
    Configure Amazon RDS mysql to store Chinese Characters
    Maximum length of a table name in MySQL
    JDBC Tutorials: Commit or Rollback transaction in finally block
    Java transient关键字使用小记
    Java构造和解析Json数据的两种方法详解二
    精选30道Java笔试题解答
    Visual Studio最好用的快捷键(你最喜欢哪个)
  • 原文地址:https://www.cnblogs.com/YXBLOGXYY/p/14648655.html
Copyright © 2011-2022 走看看