zoukankan      html  css  js  c++  java
  • 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);
                                
                        }
  • 相关阅读:
    手把手教你利用create-nuxt-app脚手架创建NuxtJS应用
    初识NuxtJS
    webpack打包Vue应用程序流程
    用选择器代替表格列的筛选功能
    Element-UI
    Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]
    Sparse Principal Component Analysis via Rotation and Truncation
    Generalized Power Method for Sparse Principal Component Analysis
    Sparse Principal Component Analysis via Regularized Low Rank Matrix Approximation(Adjusted Variance)
    Truncated Power Method for Sparse Eigenvalue Problems
  • 原文地址:https://www.cnblogs.com/z360519549/p/8565456.html
Copyright © 2011-2022 走看看