Java Bean与Map相互转换

怼烎@ 2022-05-20 04:41 274阅读 0赞
  1. import java.beans.BeanInfo;
  2. import java.beans.IntrospectionException;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. /**
  10. * 实体类与Map转换类
  11. * @author w15579
  12. *
  13. */
  14. public class BeanToMapUtil {
  15. /**
  16. * 将一个 Map 对象转化为一个 JavaBean
  17. * @param type 要转化的类型
  18. * @param map 包含属性值的 map
  19. * @return 转化出来的 JavaBean 对象
  20. * @throws IntrospectionException
  21. * 如果分析类属性失败
  22. * @throws IllegalAccessException
  23. * 如果实例化 JavaBean 失败
  24. * @throws InstantiationException
  25. * 如果实例化 JavaBean 失败
  26. * @throws InvocationTargetException
  27. * 如果调用属性的 setter 方法失败
  28. */
  29. @SuppressWarnings("rawtypes")
  30. public static Object convertMap(Class type, Map map)throws IntrospectionException, IllegalAccessException,
  31. InstantiationException, InvocationTargetException {
  32. BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
  33. Object obj = type.newInstance(); // 创建 JavaBean 对象
  34. // 给 JavaBean 对象的属性赋值
  35. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  36. for (int i = 0; i< propertyDescriptors.length; i++) {
  37. PropertyDescriptor descriptor = propertyDescriptors[i];
  38. String propertyName = descriptor.getName();
  39. if (map.containsKey(propertyName)) {
  40. // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
  41. Object value = map.get(propertyName);
  42. Object[] args = new Object[1];
  43. args[0] = value;
  44. descriptor.getWriteMethod().invoke(obj, args);
  45. }
  46. }
  47. return obj;
  48. }
  49. /**
  50. * 将一个 JavaBean 对象转化为一个 Map
  51. * @param bean 要转化的JavaBean 对象
  52. * @return 转化出来的 Map 对象
  53. * @throws IntrospectionException 如果分析类属性失败
  54. * @throws IllegalAccessException 如果实例化 JavaBean 失败
  55. * @throws InvocationTargetException 如果调用属性的 setter 方法失败
  56. */
  57. public static Map<String, Object> convertBean(Object bean)throws IntrospectionException, IllegalAccessException, InvocationTargetException {
  58. Class<? extends Object> type = bean.getClass();
  59. Map<String, Object> returnMap = new HashMap<>();
  60. BeanInfo beanInfo = Introspector.getBeanInfo(type);
  61. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  62. for (int i = 0; i< propertyDescriptors.length; i++) {
  63. PropertyDescriptor descriptor = propertyDescriptors[i];
  64. String propertyName = descriptor.getName();
  65. if (!propertyName.equals("class")) {
  66. Method readMethod = descriptor.getReadMethod();
  67. Object result = readMethod.invoke(bean, new Object[0]);
  68. if (result != null) {
  69. returnMap.put(propertyName, result);
  70. } else {
  71. returnMap.put(propertyName, "");
  72. }
  73. }
  74. }
  75. return returnMap;
  76. }
  77. }

发表评论

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

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

相关阅读