必须掌握Java的反射机制和自定义注解
package com.wyh.test; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; //注解是对应表的关联 @Retention(RetentionPolicy.RUNTIME) @interface Table { String value(); } //属性对应注解 @Retention(RetentionPolicy.RUNTIME) @interface Propety { String name(); int leng() default 0; } @Table("student") class Student { @Propety(name = "student_id", leng = 10) private String studentId; @Propety(name = "student_name") private String studentName; @Propety(name = "student_age") private String studentAge; public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentAge() { return studentAge; } public void setStudentAge(String studentAge) { this.studentAge = studentAge; } } public class Test003 { public static void main(String[] args) throws ClassNotFoundException { Class<?> forName = Class.forName("com.wyh.test.Student");//反射机制 // 获取所有当前的方法 Field[] declaredFields = forName.getDeclaredFields(); StringBuffer sf = new StringBuffer(); sf.append(" select "); for (int i = 0; i < declaredFields.length; i++) { Field field = declaredFields[i]; //获取属性上的注解 Propety propety = field.getDeclaredAnnotation(Propety.class); sf.append(propety.name()); if (i < declaredFields.length - 1) { sf.append(" , "); } } // 获取类上注解参数 Table declaredAnnotation = forName.getDeclaredAnnotation(Table.class); sf.append(" from " + declaredAnnotation.value()); System.out.println(sf.toString()); } }