map集合和javabean对象之间相互转换方法

素颜马尾好姑娘i 2021-09-17 22:16 439阅读 0赞

亲测可用, 简单易读

1、使用反射

  1. /**
  2. * 使用reflect进行转换,map集合转javabean
  3. *
  4. * @param map
  5. * @param beanClass
  6. * @return
  7. * @throws IllegalAccessException
  8. * @throws InstantiationException
  9. */
  10. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws IllegalAccessException, InstantiationException {
  11. if (map == null){
  12. return null;
  13. }
  14. Object obj = beanClass.newInstance();
  15. Field[] fields = obj.getClass().getDeclaredFields();
  16. for (Field field : fields) {
  17. int mod = field.getModifiers();
  18. if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
  19. continue;
  20. }
  21. field.setAccessible(true);
  22. field.set(obj, map.get(field.getName()));
  23. }
  24. return obj;
  25. }
  26. /**
  27. * 使用reflect进行转换,javabean对象转map集合
  28. *
  29. * @param obj
  30. * @return map集合
  31. * @throws IllegalAccessException java.lang.IllegalAccessException
  32. * @throws InstantiationException java.lang.InstantiationException
  33. */
  34. public static Map<String, Object> objectToMap(Object obj) throws Exception {
  35. if (obj == null) {
  36. return null;
  37. }
  38. Map<String, Object> map = new HashMap<String, Object>();
  39. Field[] declaredFields = obj.getClass().getDeclaredFields();
  40. for (Field field : declaredFields) {
  41. field.setAccessible(true);
  42. map.put(field.getName(), field.get(obj));
  43. }
  44. return map;
  45. }

2、使用Introspector(推荐)

  1. /**
  2. * 使用Introspector,map集合成javabean
  3. *
  4. * @param map map
  5. * @param beanClass bean的Class类
  6. * @return bean对象
  7. */
  8. public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) {
  9. if (MapUtils.isEmpty(map)) {
  10. return null;
  11. }
  12. try {
  13. T t = beanClass.newInstance();
  14. BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
  15. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  16. for (PropertyDescriptor property : propertyDescriptors) {
  17. Method setter = property.getWriteMethod();
  18. if (setter != null) {
  19. setter.invoke(t, map.get(property.getName()));
  20. }
  21. }
  22. return t;
  23. } catch (Exception ex) {
  24. log.error("########map集合转javabean出错######,错误信息,{}", ex.getMessage());
  25. throw new RuntimeException();
  26. }
  27. }
  28. /**
  29. * 使用Introspector,对象转换为map集合
  30. *
  31. * @param beanObj javabean对象
  32. * @return map集合
  33. */
  34. public static Map<String, Object> beanToMap(Object beanObj) {
  35. if (null == beanObj) {
  36. return null;
  37. }
  38. Map<String, Object> map = new HashMap<>();
  39. try {
  40. BeanInfo beanInfo = Introspector.getBeanInfo(beanObj.getClass());
  41. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  42. for (PropertyDescriptor property : propertyDescriptors) {
  43. String key = property.getName();
  44. if (key.compareToIgnoreCase("class") == 0) {
  45. continue;
  46. }
  47. Method getter = property.getReadMethod();
  48. Object value = getter != null ? getter.invoke(beanObj) : null;
  49. map.put(key, value);
  50. }
  51. return map;
  52. } catch (Exception ex) {
  53. log.error("########javabean集合转map出错######,错误信息,{}", ex.getMessage());
  54. throw new RuntimeException();
  55. }
  56. }

原贴地址:https://blog.csdn.net/hanxue6898/article/details/81187104

发表评论

表情:
评论列表 (有 0 条评论,439人围观)

还没有评论,来说两句吧...

相关阅读