Map参数转实体和HttpServletRequest 参数转实体
Map参数转实体,绝大部分类型都可以转
/**
* Map参数转实体
* @param clazz 为要转换的实体类
* @param params 参数集合
* @return
* @throws Exception
*/
public static Object paramsTransEntity(Class clazz, Map<String,Object> params) {
Field[] fields = clazz.getDeclaredFields();
try{
Object object = clazz.newInstance();
for (Field field : fields) {
String tempFieldName = field.getName();
String methodName = "set" + tempFieldName.substring(0, 1).toUpperCase() + tempFieldName.substring(1);
Object fieldVal = params.get(tempFieldName);
if (fieldVal != null) {
if(fieldVal.equals("") || fieldVal.equals("undefined")){
continue;
}
Method method = clazz.getDeclaredMethod(methodName, field.getType());
if (field.getType().getSimpleName().equals("String")) {
method.invoke(object, fieldVal);
} else if (field.getType().getSimpleName().equals("int") || field.getType().getSimpleName().equals("Integer")) {
method.invoke(object, Integer.valueOf(fieldVal.toString()));
}
else if (field.getType().getSimpleName().equals("BigDecimal")) {
method.invoke(object, new BigDecimal(fieldVal.toString()));
}
else if (field.getType().getSimpleName().equals("Double")) {
method.invoke(object, Double.valueOf(fieldVal.toString()));
}
else if (field.getType().getSimpleName().equals("Float")) {
method.invoke(object, Float.valueOf(fieldVal.toString()));
}
else if (field.getType().getSimpleName().equals("Date")) {
method.invoke(object, YTDToDate(fieldVal.toString()));
}
}
}
return object;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
HttpServletRequest 参数转实体,绝大部分类型都可以转
/**
* 请求实体转数据实体
* @param clazz 实体类
* @param request
* @return
* @throws Exception
*/
public static Object StrTransformObject(Class clazz, HttpServletRequest request) {
Field[] fields = clazz.getDeclaredFields();
try {
Object object = clazz.newInstance();
for (Field field : fields) {
String tempFieldName = field.getName();
String methodName = "set" + tempFieldName.substring(0, 1).toUpperCase() + tempFieldName.substring(1);
String fieldVal = request.getParameter(tempFieldName);
if (fieldVal != null && !fieldVal.equals("")&&!fieldVal.equals("undefined")) {
Method method = clazz.getDeclaredMethod(methodName, field.getType());
if (field.getType().getSimpleName().equals("String")) {
method.invoke(object, fieldVal);
} else if (field.getType().getSimpleName().equals("int") || field.getType().getSimpleName().equals("Integer")) {
method.invoke(object, Integer.valueOf(fieldVal));
}
else if (field.getType().getSimpleName().equals("BigDecimal")) {
method.invoke(object, new BigDecimal(fieldVal));
}
else if (field.getType().getSimpleName().equals("Double")) {
method.invoke(object, Double.valueOf(fieldVal));
}
else if (field.getType().getSimpleName().equals("Float")) {
method.invoke(object, Float.valueOf(fieldVal));
}
else if (field.getType().getSimpleName().equals("Date")) {
method.invoke(object, YTDToDate(fieldVal));
}
}
}
return object;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
日期转换
/**
* yyyy-MM-dd HH:mm:ss 转 Date
* @param dateString
* @return
* @throws Exception
*/
public static Date YTDToDate(String dateString) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse(dateString);
return date;
}catch (Exception e){
e.printStackTrace();
new RuntimeException(e);
}
return null;
}