zoukankan      html  css  js  c++  java
  • 七、反射读取注解信息

    一、ORM(ObjectRelationshipMapping) 

    ORM:对象关系映射

    写程序用 Java 来写,存数据用数据库存储

    • 类与表结构对应
    • 属性和字段对应
    • 对象和记录对应

    使用注解完成类和表结构的映射关系

    二、 功能描述

    将Java中的Student类使用第三方程序通过读取注解生成数据库中的表

    三、实现步骤

    1) 编写 Student 类

    2) 编写注解

    3) 在类中使用注解

    4) 通过解析程序将注解读取出来 (通过框架解析)

    5) 拼接 SQL 语句,使用 JDBC 到数据库中执行创建表

    student类:

     1 /**
     2  * ClassName:Student
     3  * date: 2020/4/16 11:17
     4  *
     5  * @author 王鼎禹
     6  */
     7 @WdyTable("tb_student") //数据库表名
     8 public class Student {
     9     @WdyField(columnName = "id",type = "int",length = 10)
    10     private int id;
    11 
    12     @WdyField(columnName="stuname",type="varchar",length=20)
    13     private String stuName;
    14 
    15     @WdyField(columnName="age",type="int",length=10)
    16     private int age;
    17 
    18     public Student() {
    19         super();
    20     }
    21 
    22     public int getId() {
    23         return id;
    24     }
    25 
    26     public void setId(int id) {
    27         this.id = id;
    28     }
    29 
    30     public String getStuName() {
    31         return stuName;
    32     }
    33 
    34     public void setStuName(String stuName) {
    35         this.stuName = stuName;
    36     }
    37 
    38     public int getAge() {
    39         return age;
    40     }
    41 
    42     public void setAge(int age) {
    43         this.age = age;
    44     }
    45 
    46     public Student(int id, String stuName, int age) {
    47         this.id = id;
    48         this.stuName = stuName;
    49         this.age = age;
    50     }
    51 }

    属性的注解:

     1 import java.lang.annotation.ElementType;
     2 import java.lang.annotation.Retention;
     3 import java.lang.annotation.RetentionPolicy;
     4 import java.lang.annotation.Target;
     5 
     6 /**
     7  * ClassName:Field
     8  * date: 2020/4/16 11:19
     9  *
    10  * @author 王鼎禹
    11  */
    12 @Target(ElementType.FIELD)
    13 @Retention(RetentionPolicy.RUNTIME)
    14 public @interface WdyField {//属性的注解
    15     String columnName();    //数据库中列的名称
    16     String type();  //数据库中列的类型
    17     int length();   //类型的长度
    18 }

    类的注解:

     1 /**
     2  * ClassName:Table
     3  * date: 2020/4/16 11:21
     4  *
     5  * @author 王鼎禹
     6  */
     7 @Target(ElementType.TYPE) //注解的使用范围
     8 @Retention(RetentionPolicy.RUNTIME)  //在运行时起作用
     9 public @interface WdyTable {
    10     String value();
    11 }

    实现:反射读取注解信息

     1 import java.lang.annotation.Annotation;
     2 import java.lang.reflect.Field;
     3 
     4 /**
     5  * ClassName:Test9
     6  * date: 2020/4/16 11:27
     7  *
     8  * @author 王鼎禹
     9  */
    10 public class Test9 {
    11     public static void main(String[] args) throws Exception {
    12         //(1)创建Student类的Class对象
    13         Class clazz = Class.forName("fanshe.Student");
    14         //(2)得到Student类的所有注解
    15         Annotation[] annotations = clazz.getDeclaredAnnotations();
    16         for (Annotation annotation : annotations) {
    17             System.out.println(annotation);
    18         }
    19         System.out.println("
    ----------------------------");
    20 
    21         //(3)获取指定的注解
    22         WdyTable st =(WdyTable) clazz.getDeclaredAnnotation(WdyTable.class);
    23         System.out.println(st);
    24         System.out.println("
    ----------------------------");
    25 
    26         //(4)获取属性的注解
    27         Field field = clazz.getDeclaredField("stuName");
    28         WdyField wf = field.getDeclaredAnnotation(WdyField.class);
    29         System.out.println(wf.columnName()+"--"+wf.type()+"--"+wf.length());
    30 
    31         /**拼接SQL语句  DDL ,使用JDBC在数据库中执行,创建出了一张表,tb_student,表中的列就为id,stuname,age*/
    32 
    33     }
    34 }

  • 相关阅读:
    【SQL-自动生成编号】按规则自动生成单据编号 以及并发问题_使用触发器、函数 等
    【C#-枚举】枚举的使用
    问题_VS2008和VS2012未能加载包.....以及破解VS2008方法
    【C#-算法】根据生日自动计算年龄_DataTime 的 DateDiff 方法
    【SQL-分组合并字符串】把相同分组的某个字段合并为同一个字符串(使用函数)
    【Winform-GataGridView】根据DataGridView中的数据内容设置行的文字颜色、背景色 — 根据状态变色
    【Winform-右下角弹窗】实现右下角弹窗,提示信息
    【WinForm-无边框窗体】实现Panel移动窗体,没有边框的窗体
    【Winfrom-适配窗体】 WinForm窗体及其控件的自适应,控件随着窗体变化
    【Winfrom-无边框窗体】Winform如何拖动无边框窗体?
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12711764.html
Copyright © 2011-2022 走看看