1.创建一个pojo
@Getter
@Setter
@ToString
public class User {
private Integer id;
private String name;
private Boolean isDelete;
private Date date;
}
其中利用lombok生成get/set方法,maven地址:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> </dependency>
2.反射工具类
public class ReflectUtils {
public static void setValue(Object model,String setName,String setValue){
Field[] field = model.getClass().getDeclaredFields(); // 获取实体类的所有属性,返回Field数组
try {
for (int j = 0; j < field.length; j++) { // 遍历所有属性
String name = field[j].getName(); // 获取属性的名字
if (name.equals(setName)){
name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写,方便构造get,set方法
String type = field[j].getGenericType().toString(); // 获取属性的类型
if (type.equals("class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
Method m = model.getClass().getMethod("get" + name);
String value = (String) m.invoke(model); // 调用getter方法获取属性值
if (value == null) {
m = model.getClass().getMethod("set"+name,String.class);
m.invoke(model, setValue.toString());
}
}
if (type.equals("class java.lang.Integer")) {
Method m = model.getClass().getMethod("get" + name);
Integer value = (Integer) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set"+name,Integer.class);
m.invoke(model, Integer.parseInt(setValue));
}
}
if (type.equals("class java.lang.Long")) {
Method m = model.getClass().getMethod("get" + name);
Long value = (Long) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set"+name,Integer.class);
m.invoke(model, Long.parseLong(setValue));
}
}
if (type.equals("class java.lang.Boolean")) {
Method m = model.getClass().getMethod("get" + name);
Boolean value = (Boolean) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set"+name,Boolean.class);
m.invoke(model, Boolean.parseBoolean(setValue));
}
}
if (type.equals("class java.util.Date")) {
Method m = model.getClass().getMethod("get" + name);
Date value = (Date) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set"+name,Date.class);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
m.invoke(model, sdf.parse(setValue));
}
}// 如果有需要,可以仿照上面继续进行扩充,再增加对其它类型的判断
break;//跳出较少循环
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
3.测试类
public static void main(String[] args) {
User user = new User();
//传入属性与值均为String类型,经过ReflectUtils转换为对应属性
ReflectUtils.setValue(user,"id","12");
ReflectUtils.setValue(user,"name","zhangsan");
ReflectUtils.setValue(user,"isDelete","false");
ReflectUtils.setValue(user,"date","2016-10-24 21:59:06");
User user1 = new User();
//额外功能推荐:复制相同字段与值
BeanUtils.copyBeanProp(user1,user);
System.out.println(user);
System.out.println(user1);
}
4.测试输出结果
User(id=12, name=zhangsan, isDelete=false, date=Mon Oct 24 21:59:06 CST 2016)
User(id=12, name=zhangsan, isDelete=false, date=Mon Oct 24 21:59:06 CST 2016)
参考文档:
感谢!
网友评论