default <S extends V> S save1(K key, S entity){ IgniteCache<K, V> cache = cache(); V targetObject = cache.get(key); BeanMap sourceBeanMap = BeanMap.create(entity); Map<String, Object> sourceMap = new HashMap<>(); sourceBeanMap.forEach((key1, value) -> { sourceMap.put(String.valueOf(key1), value); }); BeanMap targetBeanMap = BeanMap.create(targetObject); Map<String, Object> targetMap = new HashMap<>(); targetBeanMap.forEach((key1, value) -> { targetMap.put(String.valueOf(key1), value); }); for (String s : sourceMap.keySet()) { if(null != sourceMap.get(s)){ targetMap.put(s,sourceMap.get(s)); } } targetBeanMap.putAll(targetMap); cache.put(key,targetObject); return (S)targetObject; } default <S extends V> S save2(K key, S entity){ IgniteCache<K, V> cache = cache(); V targetObject = cache.get(key); /** * 第一次解析后,可以把方法缓存,提高性能 */ try { Method[] methods = targetObject.getClass().getMethods(); for (Method targetMethod : methods) { String name = targetMethod.getName(); if(name.startsWith("set")) { String temp = name.substring(3); String methodName = "get" + temp.substring(0,1).toUpperCase() + temp.substring(1); Method sourceGetMethod = entity.getClass().getMethod(methodName); Object sourceValue = sourceGetMethod.invoke(entity); if(null != sourceValue){ targetMethod.invoke(targetObject,sourceValue); } } } } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } cache.put(key,targetObject); return (S)targetObject; }
反射方式对于boolean型可能没有set,get方法
标签:String,get,cache,entity,beanMap,key,使用,Springcore,targetObject From: https://www.cnblogs.com/yangh2016/p/17168893.html