package com.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class select {
// person, name age
@Table(name = "person")
static class JavaPerson {
@Col(name = "name")
String javaname;
@Col(name = "age")
int javaage;
}
static <T> T select(T t) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException, SQLException {
T rs = null;
Annotation[] aa = ((Class) t).getDeclaredAnnotations();
for (Annotation a : aa) {
String tableName = (String) a.annotationType().getMethod("name").invoke(a);
String sql = "Select * from " + tableName;
// jdbc
ResultSet rss = null;
Field[] fields = ((Class) t).getDeclaredFields();
for (Field f : fields) {
String colName = (String) f.getAnnotation(Col.class).annotationType().getMethod("name").invoke(f);
String value = rss.getString(colName);
// t.setValue(value);
}
}
return rs;
}
public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException,
SecurityException, IllegalArgumentException, InvocationTargetException, SQLException {
JavaPerson rs = select(new JavaPerson());
}
}
select数据库反射机制(注解)
最新推荐文章于 2022-04-01 12:06:47 发布