|
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.test.util.BeanUtilPlus1;
public>
/**
* 测试BeanUtilsBean.getInstance().copyProperties对数据进行赋值时候的效果
* java.sql.Date不可为空值
* java.util.Date不可为空值
*
* 使用改进的BeanUtilPlus1.copyProperties
* Date为空时,直接赋空值.
*/
public static void main(String[] args) {
/**
* 初始化数据
*/
Teacher t1=new Teacher();
t1.setId(1);
t1.setName("teacher t1");
Student s1=new Student();
s1.setBooleanValue0(true);
s1.setBooleanValue1(true);
s1.setDoubleValue(1.1);
s1.setIntegerValue(12);
s1.setIntValue(123);
s1.setLongValue0(12L);
s1.setLongValue1(2L);
// s1.setSqlDateValue(new java.sql.Date(new java.util.Date().getTime()));
// s1.setUtilDateValue(new java.util.Date());
s1.setStrintValue("student s1");
s1.setTeacher(t1);
try {
//打印出所有内容
Class c =>
// Object o = c.newInstance();
Object o=s1;
Method[] ms= c.getMethods();
// System.out.println(ms.length);
System.out.println("原始数据");
for(Method m:ms){
if( m.getName().contains("get")){
System.out.println("method "+m.getName()+" : ");
System.out.println(m.invoke(o));
}
}
} catch (Exception e) {
e.printStackTrace();
}
Student s2=new Student();
try {
// BeanUtilsBean.getInstance().copyProperties(s2, s1); //调用copy方法
BeanUtilPlus1.copyProperties(s2,s1); //调用改写过的copy方法
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
Class c =>
Object o=s2;
Method[] ms= c.getMethods();
System.out.println("新数据");
for(Method m:ms){
if( m.getName().contains("get")){
System.out.println("method "+m.getName()+" : ");
System.out.println(m.invoke(o));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
|
|
|
|
|
|