1.bean转map
/**
* bean转map
* @param model
* @return
* @throws Exception
*/
public static Map<String, Object> beanToMap(Object model) throws Exception{
Map<String, Object> result = new HashMap<>();
// 获取实体类的所有属性,返回Field数组
Field[] field = model.getClass().getDeclaredFields();
// 遍历所有属性
for (int j = 0; j < field.length; j++) {
// 获取属性的名字
String key = field[j].getName();
// 将属性的首字符大写,方便构造get,set方法
String name = key.substring(0, 1).toUpperCase() + key.substring(1);
// 获取属性的类型
String type = field[j].getGenericType().toString();
// 如果type是类类型,则前面包含"class ",后面跟类名
log.info("属性为--"+type);
if (type.equals("class java.lang.String")) {
Method m = model.getClass().getMethod("get" + name);
// 调用getter方法获取属性值
String value = (String) m.invoke(model);
if (value != null) {
result.put(key,value);//往map赋值
}
}
if (type.equals("class java.lang.Integer")) {
Method m = model.getClass().getMethod("get" + name);
Integer value = (Integer) m.invoke(model);
if (value != null) {
result.put(key,value);//往map赋值
}
}
if (type.equals("class java.lang.Short")) {
Method m = model.getClass().getMethod("get" + name);
Short value = (Short) m.invoke(model);
System.out.println("数据类型为:Short");
if (value != null) {
result.put(key,value);//往map赋值
}
}
if (type.equals("class java.lang.Double")) {
Method m = model.getClass().getMethod("get" + name);
Double value = (Double) m.invoke(model);
if (value != null) {
result.put(key,value);//往map赋值
}
}
if (type.equals("class java.lang.Boolean")) {
Method m = model.getClass().getMethod("get" + name);
Boolean value = (Boolean) m.invoke(model);
if (value != null) {
result.put(key,value);//往map赋值
}
}
if (type.equals("class java.util.Date")) {
Method m = model.getClass().getMethod("get" + name);
Date value = (Date) m.invoke(model);
if (value != null) {
//时间转str
String val= DateUtil.getDateString(value);
result.put(key,val);//往map赋值
}
}
}
return result;
}
2.map转bean
/**
* Map 转bean
* @param model
* @param json
* @return
* @throws Exception
*/
public static Object mapToBean(Object model, Map<String,Object> json) throws Exception{
// 获取实体类的所有属性,返回Field数组
Field[] field = model.getClass().getDeclaredFields();
// 遍历所有属性
for (int j = 0; j < field.length; j++) {
// 获取属性的名字
String name = field[j].getName();
if(!json.containsKey(name)){
continue;
}
if((model.getClass().getName().toLowerCase() + "Id").equals(field)){
continue;
}
String originName = name;
// 将属性的首字符大写,方便构造get,set方法
name = name.substring(0, 1).toUpperCase() + name.substring(1);
// 获取属性的类型
String type = field[j].getGenericType().toString();
if (type.equals("class java.lang.String")) {
Method m = model.getClass().getMethod("set" + name, String.class);
// 调用setter方法赋值属性值
m.invoke(model, json.get(originName).toString());
}
if (type.equals("class java.lang.Integer")) {
Method m = model.getClass().getMethod("set" + name, Integer.class);
if (StringUtils.isNotEmpty(json.get(originName).toString())){
m.invoke(model, Integer.parseInt(json.get(originName).toString()));
}
}
if (type.equals("class java.lang.Short")) {
Method m = model.getClass().getMethod("set" + name, Short.class);
if (StringUtils.isNotEmpty(json.get(originName).toString())){
m.invoke(model, Short.parseShort(json.get(originName).toString()));
}
}
if (type.equals("class java.lang.Double")) {
Method m = model.getClass().getMethod("set" + name, Double.class);
if (StringUtils.isNotEmpty(json.get(originName).toString())){
m.invoke(model, Double.parseDouble(json.get(originName).toString()));
}
}
if (type.equals("class java.math.BigDecimal")) {
Method m = model.getClass().getMethod("set" + name, BigDecimal.class);
if (StringUtils.isNotEmpty(json.get(originName).toString())){
m.invoke(model, new BigDecimal(json.get(originName).toString().replaceAll(",", "")).setScale(4,BigDecimal.ROUND_HALF_UP));
}
}
if (type.equals("class java.lang.Boolean")) {
Method m = model.getClass().getMethod("set" + name, Boolean.class);
m.invoke(model, Boolean.parseBoolean(json.get(originName).toString()));
}
if (type.equals("class java.util.Date")) {
Method m = model.getClass().getMethod("set" + name, Date.class);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (StringUtils.isNotEmpty(json.get(originName).toString())&& !"null".equals(json.get(originName).toString())){
m.invoke(model, simpleDateFormat.parse(json.get(originName).toString().length() <= 10?json.get(originName).toString() + " 00:00:00" :json.get(originName).toString()));
}
}
}
return model;
}