|
@@ -0,0 +1,106 @@
|
|
|
|
+package com.sf.day24;
|
|
|
|
+
|
|
|
|
+import org.junit.Test;
|
|
|
|
+
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 通过反射调用指定的属性和方法
|
|
|
|
+ */
|
|
|
|
+public class Test01 {
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void t1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
|
|
|
|
+ //创建Student的class对象
|
|
|
|
+ Class<?> aClass = Class.forName("com.sf.day24.Student");
|
|
|
|
+ //获取属性对象 NAME id
|
|
|
|
+ Field field = aClass.getDeclaredField("id");
|
|
|
|
+ //取消java语言检查访问
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
+
|
|
|
|
+ //创建实例对象
|
|
|
|
+ Object obj = aClass.newInstance();
|
|
|
|
+
|
|
|
|
+ //给属性id设置值
|
|
|
|
+ field.set(obj,13);
|
|
|
|
+
|
|
|
|
+ //获取属性值
|
|
|
|
+ Object o = field.get(obj);
|
|
|
|
+ System.out.println(o);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 反射中方法的执行
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t2() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
|
|
|
|
+ //常见Student的class对象
|
|
|
|
+ Class<Student> clazz = Student.class;
|
|
|
|
+ //获取方法对象
|
|
|
|
+ Method method = clazz.getDeclaredMethod("setName", String.class);
|
|
|
|
+ //创建实例对象
|
|
|
|
+ Student student = clazz.newInstance();
|
|
|
|
+ //调用方法
|
|
|
|
+ method.invoke(student, "zhangsan");
|
|
|
|
+// System.out.println(zhangsan);
|
|
|
|
+ System.out.println(student);
|
|
|
|
+
|
|
|
|
+ Method getName = clazz.getDeclaredMethod("getName");
|
|
|
|
+ Object invoke = getName.invoke(student);
|
|
|
|
+ System.out.println(invoke);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 静态的调用
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
|
|
+ Class<Student> studentClass = Student.class;
|
|
|
|
+ Method getInfo = studentClass.getDeclaredMethod("getInfo", String.class);
|
|
|
|
+ Object lisa = getInfo.invoke(null, "lisa");
|
|
|
|
+ //getInfo 的返回值是void
|
|
|
|
+ System.out.println(lisa);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 看我写就行啦
|
|
|
|
+ * sql语句 SELECT id,name FROM user 查询user表里的全部记录(信息)
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t4(){
|
|
|
|
+ //创建user的class类
|
|
|
|
+ Class<User> userClass = User.class;
|
|
|
|
+ Table annotation = userClass.getAnnotation(Table.class);
|
|
|
|
+ String tableName="";
|
|
|
|
+ if(annotation !=null){
|
|
|
|
+ tableName = annotation.value();
|
|
|
|
+ }
|
|
|
|
+ //获取属性对象数组
|
|
|
|
+ Field[] declaredFields = userClass.getDeclaredFields();
|
|
|
|
+ String[] columns = new String[declaredFields.length];
|
|
|
|
+ int index= 0;
|
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
|
+ Column column = declaredField.getAnnotation(Column.class);
|
|
|
|
+ if(column != null){
|
|
|
|
+ columns[index++]= column.columnName();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String sql = "SELECT ";
|
|
|
|
+ for (int i= 0 ; i<index;i++){
|
|
|
|
+ sql+=columns[i];
|
|
|
|
+ if(i<index-1){
|
|
|
|
+ sql +=",";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ sql+=" FROM "+tableName;
|
|
|
|
+
|
|
|
|
+ // SELECT id,name FROM user
|
|
|
|
+ System.out.println("最后的完整sql是==="+sql);
|
|
|
|
+ }
|
|
|
|
+}
|