import lombok.extern.log4j.Log4j2;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author hhh
* @date 2019/6/17 11:21
* @Despriction
*/
@Log4j2
public class MapObjUtil {
/**
* getBean 将map转为Object,支持 Date/Boolean
*
* @param param
* @param clazz
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Map<String, Object> param, Class clazz)throws Exception {
Object value = null;
Class[] paramTypes = new Class[1];
Object obj = null;
//创建对象实例
obj = clazz.newInstance();
Field[] f = clazz.getDeclaredFields();
List<Field[]> flist = new ArrayList<Field[]>();
flist.add(f);
Class superClazz = clazz.getSuperclass();
while (superClazz != null) {
f = superClazz.getFields();
flist.add(f);
superClazz = superClazz.getSuperclass();
}
for (Field[] fields : flist) {
for (Field field : fields) {
String fieldName = field.getName();
value = param.get(fieldName);
if (value != null) {
paramTypes[0] = field.getType();
Method method = null;
//调用相应对象的set方法
StringBuffer methodName = new StringBuffer("set");
methodName.append(fieldName.substring(0, 1).toUpperCase());
methodName.append(fieldName.substring(1, fieldName.length()));
log.info("本次复制属性名称为:{}", paramTypes[0].getName());
method = clazz.getMethod(methodName.toString(), paramTypes);
method.invoke(obj, ConvertUtil.getValue(value.toString(), fieldName, paramTypes[0]));
}
}
}
return (T) obj;
}
}
package com.jn.ssr.superrescue.util;
import org.apache.commons.beanutils.ConvertUtils;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* @author hhh
* @date 2019/6/17 14:25
* @Despriction
*/
public class ConvertUtil {
public static<T> T getValue(String value,String fieldName,Class<T> clazz){
if (value == null) { // 如果获取参数值为null,则返回null
return null;
} else if (!value.equals("")) { // 如果获取参数值不为"",则通过convertGt方法进行类型转换后返回结果
return convertGt(value, clazz);
} else if (clazz.getName().equals(String.class.getName())) { // 如果获取参数值为""
return convertGt(value, clazz);
} else {// 如果获取参数值为"",并且clazz不是是String类型,则返回null
return null;
}
}
/**
* @param <T>
* @param value
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T convertGt(String value, Class<T> clazz) {
if (value == null) { // 如果值为null,则返回null
return null;
} else if (value.equals("")
&& !clazz.getName().equals(String.class.getName())) { // 如果value值为"",而且要转为的类型不是string类型,那么就统一返回null,也就是空字符串不能转成任何其他类型的实体,只能返回null
return null;
} else if (Date.class.getName().equalsIgnoreCase(clazz.getName())) { // 增加对从String类型到Date
return (T) convertSTD(value);
}
return (T) ConvertUtils.convert(value, clazz);
}
//日期类型的转换
private static SimpleDateFormat simpleDateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static Date convertSTD(String date){
try {
return simpleDateFormate.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String convertDTS(Date date){
return simpleDateFormate.format(date);
}
}