一、map轉實體類庫
Java中提供了許多map轉實體類的庫,如Apache BeanUtils、Spring的BeanWrapper等等。其中Apache BeanUtils是開發者使用最多的一個庫,可以輕鬆的完成從map到實體類的轉換,並且對類型轉換的支持也很完善。
public static void populate(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException {
if ((bean == null) || (properties == null)) {
return;
}
if (bean instanceof DynaBean) {
DynaProperty[] descriptors = ((DynaBean)bean).getDynaClass().getDynaProperties();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (properties.containsKey(name)) {
((DynaBean)bean).set(name, properties.get(name));
}
}
} else {
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean);
for (int i = 0; i 0) && ((types[0] instanceof Class)) && (!(types[0] instanceof Enum))) {
Class parameterType = (Class)types[0];
if (parameterType.isPrimitive()) {
continue;
}
}
}
try {
BeanUtils.setProperty(bean, name, value);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}
二、map轉實體類 mybatisplus
Mybatis-plus是Mybatis的增強工具,其中包含了對map轉實體類的支持,可以很方便地將map轉換為需要的實體類,具體代碼如下:
Map map = new HashMap();
map.put("id", 1);
map.put("name", "Tom");
User user = MapUtil.toBean(map, new User());
三、map轉實體類對象
在Java中,可以通過反射實現將map轉換為實體類對象,比較常見的做法是使用BeanUtils,調用其populate方法進行轉換,具體代碼如下:
Map map = new HashMap();
map.put("id", 1);
map.put("name", "Tom");
User user = new User();
BeanUtils.populate(user, map);
四、map轉實體類空指針
在進行map轉換時,有的時候可能會遇到空指針異常,這是因為map中不存在需要轉換的屬性,可以通過以下方式解決:
// 判斷屬性是否存在
if(map.containsKey("name")){
user.setName(map.get("name"));
}
五、map轉實體類工具類
為了方便使用,可以編寫一個工具類,封裝map轉實體類的過程,具體代碼如下:
public class MapUtil {
public static T toBean(Map map, Class clazz) {
try {
T obj = clazz.newInstance();
BeanUtils.populate(obj, map);
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
六、map轉實體類全是string
在進行map轉換時,有的時候map中的屬性都是字元串類型,但實體類中的屬性類型卻不是,可以通過以下方式解決:
Map map = new HashMap();
map.put("id", "1");
map.put("name", "Tom");
User user = new User();
for (Map.Entry entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
try {
Field field = User.class.getDeclaredField(key);
field.setAccessible(true);//設置為可訪問
Class type = field.getType();
if (type == String.class) {
field.set(user, value);
} else if (type == Integer.class) {
field.set(user, Integer.parseInt(value));
} else if (type == BigDecimal.class) {
field.set(user, new BigDecimal(value));
}
//...
} catch (Exception e) {
e.printStackTrace();
}
}
七、實體類轉map
實體類轉map,可以通過反射獲取實體類的所有屬性,然後將屬性名稱作為key,屬性值作為value存入map中,具體代碼如下:
public static Map objectToMap(Object obj) {
if (obj == null)
return null;
Map map = new LinkedHashMap();
Class clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return map;
}
八、map轉換成實體類
如果需要將一個map中的多個值轉換成同一個實體類對象,可以使用BeanUtils.populate方法,具體代碼如下:
Map map = new HashMap();
map.put("id", 1);
map.put("name", "Tom");
map.put("age", 18);
User user1 = new User();
User user2 = new User();
BeanUtils.populate(user1, map);
BeanUtils.populate(user2, map);
九、map轉成實體類對象
有時候需要將一個map轉換成某個實體類對象的屬性,可以通過反射設置屬性值的方式實現。具體代碼如下:
Map map = new HashMap();
map.put("id", 1);
map.put("name", "Tom");
User user = new User();
for (Map.Entry entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
try {
Field field = User.class.getDeclaredField(key);
field.setAccessible(true); //設置為可訪問
Class type = field.getType();
if (type == String.class) {
field.set(user, value.toString());
} else if (type == Integer.class) {
field.set(user, Integer.parseInt(value.toString()));
} else if (type == BigDecimal.class) {
field.set(user, new BigDecimal(value.toString()));
}
//...
} catch (Exception e) {
e.printStackTrace();
}
}
十、總結
通過本文的介紹,我們了解了Java中如何將map轉換為實體類對象。其中,Apache BeanUtils和Spring的BeanWrapper提供了更加便捷的map轉實體類的方法,而Mybatis-plus則是在Mybatis基礎上提供了更多功能的增強工具。同時,我們還介紹了如何處理map中屬性為字元串類型的情況,以及如何將實體類對象轉換成map,以及map之間的轉換。希望本文對讀者有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286477.html
微信掃一掃
支付寶掃一掃