• 七、反射读取注解信息


    一、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 }

  • 相关阅读:
    【CentOS】CentOS7开放及查看端口
    【nginx】配置https 证书生成的方法
    【MacOs】 Royal TSX SSH 和 FTP 中文乱码
    【PHP】thinkphp3.2.5
    【TCP/IP】入门学习笔记 五
    【TCP/IP】入门学习笔记 四
    HTTP
    【PHP】高并发和大流量的解决方案(思路)
    react多级路由 重定向与404定义
    react自定义导航组件 路由参数
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12711764.html
Copyright © 2020-2023  润新知