表 service_goods_base 字段如下:
傳入的json 字符串: servicePictureArray : [{"picServiceUrl": "http://qimg.app.yiguanjiaclub.org/20180308/a48210dc7bfe4b34b3d7de114ef01f85","mainPage": "1"}]
方法如下:
if(StringUtil.isNotEmpty(servicePictureArray)) { JSONArray array = JSONArray.fromObject(servicePictureArray); List<ServiceGoodsPicture> list = new ArrayList<ServiceGoodsPicture>(); for (int i = 0; i < array.size(); i++) { Map<String, Object> object = (Map<String, Object>) array .get(i); ServiceGoodsPicture entity = ClassUtil.mapToEntity(ServiceGoodsPicture.class, object); list.add(entity); } List<ServiceGoodsPicture> picList=serviceGoodsPictureService.saveServicePicBase(list, serviceId); sgbEntity.setPicList(picList); }
先用 JSONArray.fromObject(servicePictureArray) 把json 字符串轉化成 一個json數組array。 對應一個 bean 的 list 。
獲取array數組中的每個元素,保存在 object 中。 再通過 mapToEntity 方法 把 map 的對象 object 轉化成 表的對象 entity 。 並且把這個對象保存在list 中。
調用另一個service 的方法,獲取list 的每個元素,保存在 bean 的 picture 對象中,並給picture 對象其他的屬性賦值。最后 調用mybatis 的方法, 插入picture 對象。保存在表中。
public List<ServiceGoodsPicture> saveServicePicBase(List<ServiceGoodsPicture> list, String goodsId) { List<ServiceGoodsPicture> restList=new ArrayList<ServiceGoodsPicture>(); for(int i = 0; i < list.size();i++) { ServiceGoodsPicture restPic=new ServiceGoodsPicture(); ServiceGoodsPicture picture = list.get(i); picture.setSid(UUIDUtil.getUUID()); picture.setPicCreateTime(new Date()); picture.setServiceId(goodsId); picture.setSort(i); serviceGoodsPictureMapper.insertSelective(picture); } return restList; }
map 與對象的轉化函數:
public static <T> T mapToEntity(Class<T> objClass,Map<String,Object> map){ T entity = null; try { entity = objClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(entity.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (map.containsKey(key)) { Object value = map.get(key); if(value instanceof JSONNull){ value = null; } Object convertValue = value; // 得到property對應的setter方法 Method setter = property.getWriteMethod(); Class<?> propertyType = property.getPropertyType(); if ((propertyType == Double.class) || (propertyType == Double.TYPE)) { if (!(value instanceof Double))
if (!(value.toString()).matches("[0-9.]+"))
convertValue = 0;
else
convertValue = Double.valueOf(value.toString());
} else if ((propertyType == Long.class) || (propertyType == Long.TYPE)) { if (!(value instanceof Long)) convertValue = 0.0; } else if ((propertyType == Integer.class) || (propertyType == Integer.TYPE)) { if (!(value instanceof Integer) ) if (!(value.toString()).matches("[0-9]+")) convertValue = 0; else convertValue = Integer.valueOf((String) value); } else if ((propertyType == Float.class) || (propertyType == Float.TYPE)) { if (!(value instanceof Float)) convertValue = 0.0; } else if ((propertyType == Boolean.TYPE) || (propertyType == Boolean.class)) { if (!(value instanceof Boolean)) { if (("1".equals(value)) || ("Y".equalsIgnoreCase(value + "")) || ("Yes".equalsIgnoreCase(value + ""))) convertValue = true; else convertValue = false; } } else if (propertyType == Date.class) { if ((!(value instanceof Date)) && (value != null)) { convertValue = null; } } else if (value instanceof net.sf.json.JSONNull){ } setter.invoke(entity, convertValue); } } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IntrospectionException e) { e.printStackTrace(); } return entity; } public static Map<String, Object> entityToMap(Object obj){ if(obj == null) return null; Map<String, Object> map = new HashMap<String, Object>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (key.compareToIgnoreCase("class") == 0) { continue; } Method getter = property.getReadMethod(); Object value = getter!=null ? getter.invoke(obj) : null; map.put(key, value); } } catch (Exception e) { } return map; }
如果 bean 的 propertyType 是 Integer ,map 中的value 傳的不是 Integer : 則做一下判斷:
如果value 是只包括 0-9 的字符,則把 value 轉化成 Integer ,賦值給 convertValue , 否則 convertValue 賦值是 0.
else if ((propertyType == Integer.class) || (propertyType == Integer.TYPE)) {
if (!(value instanceof Integer) ) if (!(value.toString()).matches("[0-9]+")) convertValue = 0; else convertValue = Integer.valueOf((String) value); }