Java比较两个bean数据差异

/**  
 * @Title: CompareObjUtil.java
 * @Package cc.messcat.common.util
 * @Description: TODO
 * @author limh
 * @date 2017年3月2日
 */
package cc.messcat.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import cc.messcat.vo.Comparison;

/**
 * ClassName: 对象比较类 
 * @Description: TODO
 * @author limh
 * @date 2017年3月2日
 */
public class CompareObjUtil {

    public static List<Comparison> compareObj(Object beforeObj,Object afterObj) throws Exception{
        List<Comparison> differents = new ArrayList<Comparison>();
        
        if(beforeObj == null) throw new Exception("原对象不能为空");
        if(afterObj == null) throw new Exception("新对象不能为空");
        if(!beforeObj.getClass().isAssignableFrom(afterObj.getClass())){
            throw new Exception("两个对象不相同,无法比较");
        }
        
        //取出属性
        Field[] beforeFields = beforeObj.getClass().getDeclaredFields();
        Field[] afterFields = afterObj.getClass().getDeclaredFields();
        Field.setAccessible(beforeFields, true); 
        Field.setAccessible(afterFields, true);
        
        //遍历取出差异值
        if(beforeFields != null && beforeFields.length > 0){
            for(int i=0; i<beforeFields.length; i++){
                Object beforeValue = beforeFields[i].get(beforeObj);
                Object afterValue = afterFields[i].get(afterObj);
                    if((beforeValue != null && !"".equals(beforeValue) && !beforeValue.equals(afterValue)) || ((beforeValue == null || "".equals(beforeValue)) && afterValue != null)){
                        Comparison comparison = new Comparison();
                        comparison.setField(beforeFields[i].getName());
                        comparison.setBefore(beforeValue);
                        comparison.setAfter(afterValue);
                        differents.add(comparison);
                    }
            }
        }
        
        return differents;
    }
}
 

 

/**  
 * @Title: Comparison.java
 * @Package cc.messcat.vo
 * @Description: TODO
 * @author limh
 * @date 2017年3月2日
 */
package cc.messcat.vo;

/**
 * ClassName: 对照实体 
 * @Description: TODO
 * @author limh
 * @date 2017年3月2日
 */
public class Comparison {
    
    //字段
    private String Field;
    //字段旧值
    private Object before;
    //字段新值
    private Object after;
    
    /**
     * @return the field
     */
    public String getField() {
        return Field;
    }
    /**
     * @param field the field to set
     */
    public void setField(String field) {
        Field = field;
    }
    /**
     * @return the before
     */
    public Object getBefore() {
        return before;
    }
    /**
     * @param before the before to set
     */
    public void setBefore(Object before) {
        this.before = before;
    }
    /**
     * @return the after
     */
    public Object getAfter() {
        return after;
    }
    /**
     * @param after the after to set
     */
    public void setAfter(Object after) {
        this.after = after;
    }
    
}
 

 

转载于:https://my.oschina.net/u/2484728/blog/849545

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值