1、创建entity
package com.jeff.entity;
public class User {
private String userName;
private String password;
private Integer age;
public User() {
super();
}
public User(String userName, String password, Integer age) {
super();
this.userName = userName;
this.password = password;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + ", age=" + age + "]";
}
}
2、创建utils(EntityUtils.java)
package com.jeff.utils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class EntityUtils {
/**
*
* @description: 实体类转Map
* @author: Jeff
* @date: 2019年10月29日
* @param object
* @return
*/
public static Map<String, Object> entityToMap(Object object) {
Map<String, Object> map = new HashMap<>();
for (Field field : object.getClass().getDeclaredFields()) {
try {
boolean flag = field.isAccessible();
field.setAccessible(true);
Object o = field.get(object);
map.put(field.getName(), o);
field.setAccessible(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
/**
*
* @description: Map转实体类
* @author: Jeff
* @date: 2019年10月29日
* @param <T>
* @param map 需要初始化的数据,key字段必须与实体类的成员名字一样,否则赋值为空
* @param entity 需要转化成的实体类
* @return
*/
public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
T t = null;
try {
t = entity.newInstance();
for (Field field : entity.getDeclaredFields()) {
if (map.containsKey(field.getName())) {
boolean flag = field.isAccessible();
field.setAccessible(true);
Object object = map.get(field.getName());
if (object != null && field.getType().isAssignableFrom(object.getClass())) {
field.set(t, object);
}
field.setAccessible(flag);
}
}
return t;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return t;
}
}
3、测试-创建Map转实体
package com.jeff;
import java.util.HashMap;
import java.util.Map;
import com.jeff.entity.User;
import com.jeff.utils.EntityUtils;
public class TestCopyObj3 {
public static void main(String[] args) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("userName", "Jeff");
map.put("password", "123456");
map.put("age", 18);
System.out.println("Map对象:" + map);
User user = EntityUtils.mapToEntity(map, User.class);
System.out.println("User对象:" + user);
}
}
4、创建实体类转Map
package com.jeff;
import java.util.Map;
import com.jeff.entity.User;
import com.jeff.utils.EntityUtils;
public class TestCopyObj4 {
public static void main(String[] args) throws Exception {
User user = new User("Jeff", "123456", 18);
System.out.println("User对象:" + user);
Map<String, Object> map = EntityUtils.entityToMap(user);
System.out.println("Map对象:" + map);
}
}
原文链接:https://blog.csdn.net/m0_67394006/article/details/123752723