java前端传入的json字符串保存到表中的方法

 表 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); }

转载于:https://www.cnblogs.com/z360519549/p/8565456.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值