前言:
本文内容:获取注解信息
推荐免费Java注解和反射讲解视频:【狂神说Java】注解和反射_哔哩哔哩_bilibili
Java注解和反射(AAR)笔记代码下载地址:
蓝奏云:下载地址 密码:joker
百度云:下载地址 提取码:0y6a
获取注解信息
反射操作注解
- getAnnotations
- getAnnotation
练习:ORM
-
了解什么是ORM
- Object relationship Mapping --> 对象关系映射
1 2 3 4 5
| class Student1{ int id; String name; int age; }
|
id |
name |
age |
001 |
张三 |
22 |
002 |
李四 |
23 |
-
类和表结构对应
-
属性和字段对应
-
对象和记录对应
-
要求:利用注解和反射完成类和表结构的银蛇关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| package com.jokerdig.reflection;
import java.lang.annotation.*; import java.lang.reflect.Field;
public class Demo10 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class<?> c1 = Class.forName("com.jokerdig.reflection.Student1"); Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } Stu annotation = (Stu) c1.getAnnotation(Stu.class); String value = annotation.value(); System.out.println(value);
Field name = c1.getDeclaredField("name"); FieldStu annotation1 = name.getAnnotation(FieldStu.class); System.out.println(annotation1.colunName()); System.out.println(annotation1.type()); System.out.println(annotation1.length()); } } @Stu("db_student") class Student1{ @FieldStu(colunName = "db_id",type = "int",length = 10) private int id; @FieldStu(colunName = "db_age",type = "int",length = 10) private int age; @FieldStu(colunName = "db_name",type = "varchar",length = 3) private String name;
public Student1() { }
public Student1(int id, int age, String name) { this.id = id; this.age = age; this.name = name; }
@Override public String toString() { return "Student1{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface Stu{ String value(); }
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface FieldStu{ String colunName(); String type(); int length(); }
|