1.业务场景中需要去把一个实体拷贝到另外一个实体中去做出保存,反射对象的属性和值和需要拷贝对象的值去做出比较这个时候就不用一个一个值set方便于别的方法再次利用
/* * 该方法用于对象赋值 * @param source 拷贝的对象 * @param dest 拷贝到的对象 * * */ public static Object Copy(Object source, Object dest) throws Exception { // 获取属性 BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(),Object.class); PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors(); BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(),Object.class); PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors(); try { for (int i = 0; i < sourceProperty.length; i++) { for (int j = 0; j < destProperty.length; j++) { if (sourceProperty[i].getName().equals(destProperty[j].getName())) { if (sourceProperty[i].getReadMethod().invoke(source) != null){ // 调用source的getter方法和dest的setter方法 if (destProperty[j].getReadMethod().getReturnType().getName().indexOf("Date") >= 0){ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Object invoke = sourceProperty[i].getReadMethod().invoke(source); if (invoke != null){ String invokeString = invoke.toString(); try { Date date = dateFormat.parse(dateFormat.format(invoke)); destProperty[j].getWriteMethod().invoke(dest,date); }catch (Exception e){ try { e.getMessage(); String dal = invokeString.replaceAll("T", " "); Date date = dateFormat.parse(dal); destProperty[j].getWriteMethod().invoke(dest,date); }catch (Exception e1){ e1.getMessage(); } } } }else if (destProperty[j].getReadMethod().getReturnType().getName().indexOf("Double") >= 0){ Object invoke = sourceProperty[i].getReadMethod().invoke(source); String dubboString = invoke.toString(); if (!StringUtils.isEmpty(dubboString)){ destProperty[j].getWriteMethod().invoke(dest,Double.valueOf(dubboString)); } }else{ destProperty[j].getWriteMethod().invoke(dest,sourceProperty[i].getReadMethod().invoke(source)); } } } } } return dest; } catch (Exception e) { throw new Exception("属性复制失败:" + e.getMessage()); } }
2.业务场景中由于该业务场景中没有配置多数据源的形式。导致使用原始的JDBC去进行增加修改。这个时候可以利用反射的原理获取到实体对象的值,然后跟数据库表做出比较。进行数据的填充
/* * 用于获取到该对象的所有内容进行反射 * */ public static Map<String, String> classReflect(Object model,Object dest){ if (model != null){ Map<String,String> map = new HashMap<String,String>(); StringBuffer sqlStr = new StringBuffer(); StringBuffer valueSql = new StringBuffer(); Field[] field = model.getClass().getDeclaredFields(); // 获取实体类的所有属性,返回Field数组 for (int j = 0; j < field.length; j++) { // 遍历所有属性 String name = ""; String type = ""; name = field[j].getName(); name = name.substring(0, 1).toUpperCase() + name.substring(1); type = field[j].getGenericType().toString(); if (!StringUtils.isEmpty(name) && !"Dkdm".equals(name)){ try { String nameUpper = name.toUpperCase(); nameUpper = nameUpper.toUpperCase(); Method m = model.getClass().getMethod("get" + nameUpper); Object value = m.invoke(model); if (value != null && !StringUtils.isEmpty(value.toString()) && !value.toString().equals("/") && !value.toString().equals("无")){ Field[] jsydArray = dest.getClass().getDeclaredFields(); boolean check = true; for (int i = 0; i < jsydArray.length;i++){ String dateName = jsydArray[i].getName(); dateName = dateName.substring(0, 1).toUpperCase() + dateName.substring(1); if (dateName.toUpperCase().equals(name.toUpperCase())){ check = false; break; } } if (check){ continue; } if (StringUtils.isEmpty(sqlStr.toString())){ sqlStr.append(name); }else{ sqlStr.append(","+name); } if (type.equals("class java.lang.String")) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (StringUtils.isEmpty(valueSql.toString())){ try { String date = value.toString().replaceAll("T"," "); value = simpleDateFormat.format(date); valueSql.append("to_date('"+value+"','yyyy-mm:dd HH24:Mi:ss')"); continue; } catch (Exception e) { try { String date = value.toString().replaceAll("T"," "); Date parse = simpleDateFormat.parse(date); valueSql.append("to_date('"+value+"','yyyy-mm:dd HH24:Mi:ss')"); continue; }catch (Exception e1){ valueSql.append("'"+value+"'"); continue; } } }else{ try { String date = value.toString().replaceAll("T"," "); value = simpleDateFormat.format(date); valueSql.append(",to_date('"+value+"','yyyy-mm:dd HH24:Mi:ss')"); continue; } catch (Exception e) { try { String date = value.toString().replaceAll("T"," "); simpleDateFormat.parse(date); value = value.toString().replaceAll("T"," "); valueSql.append(",to_date('"+value+"','yyyy-mm:dd HH24:Mi:ss')"); continue; }catch (Exception e1){ valueSql.append(",'"+value+"'"); continue; } } } } if (type.equals("class java.lang.Double")) { if (StringUtils.isEmpty(valueSql.toString())){ valueSql.append(value); continue; }else{ valueSql.append(","+value); continue; } } if (type.equals("class java.lang.int")) { if (StringUtils.isEmpty(valueSql.toString())){ valueSql.append(value); continue; }else{ valueSql.append(","+value); continue; } } if (type.equals("class java.util.Date")) { if (StringUtils.isEmpty(valueSql.toString())){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { value = simpleDateFormat.format(value); valueSql.append("to_date('"+value+"','yyyy-mm:dd HH24:Mi:ss')"); continue; } catch (Exception e) { String format = simpleDateFormat.format(new Date()); valueSql.append("to_date('"+format+"','yyyy-mm:dd HH24:Mi:ss')"); continue; } }else{ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { value = simpleDateFormat.format(value); value = value.toString().replaceAll("T"," "); valueSql.append(",to_date('"+value+"','yyyy-mm:dd HH24:Mi:ss')"); continue; } catch (Exception e) { String format = simpleDateFormat.format(new Date()); value = value.toString().replaceAll("T"," "); valueSql.append(",to_date('"+format+"','yyyy-mm:dd HH24:Mi:ss')"); continue; } } }else{ if (StringUtils.isEmpty(valueSql.toString())){ valueSql.append(value); continue; }else{ valueSql.append(","+value); continue; } } }else if (!StringUtils.isEmpty(name) && name.equals("BSM")){ if (StringUtils.isEmpty(sqlStr.toString())){ sqlStr.append(name); }else{ sqlStr.append(","+name); } if (StringUtils.isEmpty(valueSql.toString())){ valueSql.append(UUID.randomUUID().toString()); continue; }else{ valueSql.append(","+UUID.randomUUID().toString()); continue; } }else{ continue; } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } map.put("key",sqlStr.toString()); map.put("value",valueSql.toString()); return map; } return null; }