首页 / JAVA / java-使用反射实现ORM映射
java-使用反射实现ORM映射
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-使用反射实现ORM映射,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2315字,纯文字阅读大概需要4分钟。
内容图文
![java-使用反射实现ORM映射](/upload/InfoBanner/zyjiaocheng/802/9547752030f0471f81e0e9df6bf67e6d.jpg)
自定义两个注解
1 package com.moon.ROM; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @Target(ElementType.FIELD) 9 @Retention(RetentionPolicy.RUNTIME) 10 public @interface SexFileAnnotation { 11 String name(); 12 String type(); 13 int length(); 14 15 }
1 package com.moon.ROM; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @Target(ElementType.TYPE) 9 @Retention(RetentionPolicy.RUNTIME) 10 public @interface SexTable { 11 String value(); 12 13 }
1 package com.moon.ROM; 2 @SexTable("tb_student") 3 public class SxtStudent { 4 @SexFileAnnotation(name ="studentName" ,type = "vachar",length = 10) 5 private String studentName; 6 @SexFileAnnotation(name="age",type = "int",length = 3) 7 private int age; 8 @SexFileAnnotation(name = "id",type = "int",length = 5) 9 private int id; 10 11 public String getStudentName() { 12 return studentName; 13 } 14 15 public void setStudentName(String studentName) { 16 this.studentName = studentName; 17 } 18 19 public int getAge() { 20 return age; 21 } 22 23 public void setAge(int age) { 24 this.age = age; 25 } 26 27 public int getId() { 28 return id; 29 } 30 31 public void setId(int id) { 32 this.id = id; 33 } 34 }
1 package com.moon.ROM; 2 3 import java.lang.reflect.Field; 4 5 public class Test { 6 public static void main(String[] args) { 7 try { 8 Class clazz = Class.forName("com.moon.ROM.SxtStudent"); 9 // Annotation[] annotations = clazz.getAnnotations(); 10 /* for (Annotation annotation : annotations) { 11 System.out.println(annotation); 12 13 }*/ 14 SexTable st = (SexTable) clazz.getAnnotation(SexTable.class); 15 Field f = clazz.getDeclaredField("studentName"); 16 SexFileAnnotation sexf = f.getAnnotation(SexFileAnnotation.class); 17 System.out.println(sexf.name()+"---"+sexf.type()+"---"+sexf.length()); 18 19 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 24 } 25 }
内容总结
以上是互联网集市为您收集整理的java-使用反射实现ORM映射全部内容,希望文章能够帮你解决java-使用反射实现ORM映射所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。