当前位置:网站首页>反射操作注解
反射操作注解
2022-06-22 06:10:00 【酷小亚】
练习:ORM(对象关系映射)

代码如下:
/** * 练习反射操作注解 */
public class Demo12 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("www.jin.Student2");
//提供反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的value的值
TableJin tableJin = (TableJin) c1.getAnnotation(TableJin.class);
String value = tableJin.value();
System.out.println(value);
//获得类指定的注解
Field f = c1.getDeclaredField("id");
FieldJin annotation = f.getAnnotation(FieldJin.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@TableJin("db_student")
class Student2{
@FieldJin(columnName = "db_id",type = "int",length = 10)
private int id;
@FieldJin(columnName = "db_age",type = "int",length = 10)
private int age;
@FieldJin(columnName = "db_name",type = "varchar",length = 3)
private String name;
public Student2() {
}
public Student2(int id, int age, String name) {
this.id = id;
this.age = age;
this.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;
}
@Override
public String toString() {
return "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableJin{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldJin{
String columnName();
String type();
int length();
}
运行结果:

边栏推荐
- pgsql批量插入
- D3D learning notes (1) - Introduction to the use conditions of autodraw at so stage
- 878. 第 N 个神奇数字 数学+二分
- Use of idea plug-in EASYCODE
- Research on automatic landing control system of carrier aircraft
- 【Rust笔记】03-引用
- Improve your game‘s performance
- 单细胞论文记录(part8)--Cell2location maps fine-grained cell types in spatial transcriptomics
- [Key review of cloud computing]
- 从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
猜你喜欢

Surfer格网文件裁剪

MFC tabctrl control to modify label size

Unity development - scene asynchronous loading

Keil调试时设置断点的高级用法

e. Hash & oldcap = = 0 detailed interpretation

Simple use of idea plug-in easy code

reduce_sum()中的reduction_indices

402 string (Title: Sword finger offer58 ii. left rotation string, 28. implementation of strstr(), 459 Repeated substrings)

Single precision, double precision and precision (Reprint)

PyG教程(7):剖析邻域聚合
随机推荐
idea插件Easy Code的简单使用
【NAND文件系统】UBI介绍
[Examen des points clés de l'informatique en nuage]
经验模式分解(EMD)和希尔伯特-黄变换(HHT)
Array and foreach traversal in C #
牛客-TOP101-BM27
为什么我选择 Rust
【自己动手写CPU】异常相关指令的实现
C#中的泛型
e. Hash & oldcap = = 0 detailed interpretation
【Rust笔记】04-表达式
生信可视化(part4)--相关性图
Flink核心功能和原理
BinaryFormatter saving and loading game data for unity
[Key review of cloud computing]
单细胞论文记录(part7)--DL and alignment of spatially resolved single-cell transcriptomes with Tangram
线程池的三大方法
论文实验记录(part1)--Detection ofnatural clusters via S-DBSCAN a Self-tuning version of DBSCAN
MFC tabctrl control to modify label size
Single cell literature learning (Part2) -- stplus: a reference based method for the exact enhancement of St