/** * description * 找出两个对象改变的内容 * @Author 张宝平 * @Date 2017/5/9 15:24 * @param oldBean,newBean * @return */ public Map<String ,String> contrastObj(Object oldBean, Object newBean) { Map<String,String> resultMap = new HashMap<>(); try { Class clazz = oldBean.getClass(); Field[] fields = oldBean.getClass().getDeclaredFields(); for (Field field : fields) { PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); Method getMethod = pd.getReadMethod(); Object o1 = getMethod.invoke(oldBean); Object o2 = getMethod.invoke(newBean); if(o1==null || o2 == null){ continue; } if (!o1.toString().equals(o2.toString())) { resultMap.put(field.getName(),"变更前:"+o1.toString()+" 变更后:"+o2.toString()); } } } catch (Exception e) { e.printStackTrace(); } return resultMap; }
对象修改前后对比
最新推荐文章于 2021-07-09 16:47:52 发布