package com.wkcto.jdbc.test;
import java.lang.reflect.Method;
public class Test02 {
public static void main(String[] args) throws Exception{
// 已知
/*
String className = "com.wkcto.jdbc.domain.Student"; // 完整类名
String propertyName = "id"; // 属性名
String propertyValue = "100"; // 属性值
*/
// mybatis框架使用过程当中我们程序员是需要提供sql语句:
// sql : select id,name,birth from tbl_student;
/*
+----+----------+------------+
| id | name | birth |
+----+----------+------------+
| 1 | zhangsan | 1980-10-11 |
| 2 | lisi | 1981-10-11 |
+----+----------+------------+
*/
String className = "com.wkcto.jdbc.domain.User"; // 完整类名
String propertyName = "sid"; // 属性名
String propertyValue = "1"; // 属性值
// 通过反射机制,创建对象,给属性赋值
Class c = Class.forName(className);
Object obj = c.newInstance();
String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
Method method = c.getDeclaredMethod(methodName, String.class);
method.invoke(obj, propertyValue);
// 输出
System.out.println(obj);
}
}