欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

反射比较两个对象属性值是否有修改

程序员文章站 2022-05-28 17:00:04
...
springMVC Control


@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(RedirectAttributes redirectAttr,
			GoodsSupplierPrice gsp,
			@ModelAttribute(App.SESSION_USER_KEY) SessionUser sessionUser) {
		if (gsp.getId() == null) {
			gsp.setOperUserId(sessionUser.getUserId());
			this.goodsSupplierPriceService.save(gsp);
			operateLogService.insert("insert", "插入-采购报价SKU:"+gsp.getGoodsSku(), sessionUser.getUserId(), "采购管理", "采购报价管理");	
		} else {
			GoodsSupplierPrice gspOld = this.goodsSupplierPriceService.load(gsp.getId());
			try{
				String editMsg= CommonUtil.getEditMsg(gspOld,gsp,gsp.getClass().getName());
			    this.goodsSupplierPriceService.update(gsp);
			    operateLogService.insert("update", "更新-采购报价SKU:"+gsp.getGoodsSku()+"["+editMsg+"]", sessionUser.getUserId(), "采购管理", "采购报价管理");	
			 }catch(Exception e){
				e.printStackTrace();
			}
		}



实体对象

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.xuanfeiyang.annotations.Column;

public class GoodsSupplierPrice {
	private Integer id;

	private Integer supplierId;
    
	@Column(Desc = "SKU")
	private String goodsSku;
	
	@Column(Desc = "商品名称")
	private String goodsName;
	
	@Column(Desc = "商品单位")
	private String goodsUnit;
	
	@Column(Desc = "采购量从")
	private Integer countMin;
	
	@Column(Desc = "采购量到")
	private Integer countMax;
	
	@Column(Desc = "报价")
	private BigDecimal price;

	@JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date startDate;

	@JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date endDate;
	
	@Column(Desc = "采购周期")
	private Short buyPeriod;
	
	@Column(Desc = "优先级")
	private Short priority;
	
	@Column(Desc = "备注")
	private String note;

	private Integer operUserId;

	private Date createdTime;

	private Date lastUpdatedTime;
	
	// 附件字段:供应商名称
	private String supplierName;
	
	// 附件字段:维护人名称
	private String operUserName;

	public String getSupplierName() {
		return supplierName;
	}

	public void setSupplierName(String supplierName) {
		this.supplierName = supplierName;
	}

	public String getOperUserName() {
		return operUserName;
	}

	public void setOperUserName(String operUserName) {
		this.operUserName = operUserName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(Integer supplierId) {
		this.supplierId = supplierId;
	}

	public String getGoodsSku() {
		return goodsSku;
	}

	public void setGoodsSku(String goodsSku) {
		this.goodsSku = goodsSku == null ? null : goodsSku.trim();
	}

	public String getGoodsName() {
		return goodsName;
	}

	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName == null ? null : goodsName.trim();
	}

	public String getGoodsUnit() {
		return goodsUnit;
	}

	public void setGoodsUnit(String goodsUnit) {
		this.goodsUnit = goodsUnit == null ? null : goodsUnit.trim();
	}

	public Integer getCountMin() {
		return countMin;
	}

	public void setCountMin(Integer countMin) {
		this.countMin = countMin;
	}

	public Integer getCountMax() {
		return countMax;
	}

	public void setCountMax(Integer countMax) {
		this.countMax = countMax;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Short getBuyPeriod() {
		return buyPeriod;
	}

	public void setBuyPeriod(Short buyPeriod) {
		this.buyPeriod = buyPeriod;
	}

	public Short getPriority() {
		return priority;
	}

	public void setPriority(Short priority) {
		this.priority = priority;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note == null ? null : note.trim();
	}

	public Integer getOperUserId() {
		return operUserId;
	}

	public void setOperUserId(Integer operUserId) {
		this.operUserId = operUserId;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public Date getLastUpdatedTime() {
		return lastUpdatedTime;
	}

	public void setLastUpdatedTime(Date lastUpdatedTime) {
		this.lastUpdatedTime = lastUpdatedTime;
	}
}




工具类


/**
     * 数据修改对比统计
     * @param oldT 修改前
     * @param newT 修改后
     * @param className 类名
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> String getEditMsg(T oldT, T newT, String className) throws Exception{
        Class onwClass = Class.forName(className);
        StringBuffer editMsg = new StringBuffer();
        Field[] fields = onwClass.getDeclaredFields();
        for (Field f : fields) {
            // 过滤 static、 final、private static final字段
            if (f.getModifiers() == 16 || f.getModifiers() == 8
                    || f.getModifiers() == 26) {
                continue;
            }
            com.xuanfeiyang.annotations.Column annotationColumn = f.getAnnotation(com.xuanfeiyang.annotations.Column.class);
            if (annotationColumn == null) {
                continue;
            }
            Object oldV = ReflectUtil.getValueMethod(oldT,f.getName(),f.getType());
            Object newV = ReflectUtil.getValueMethod(newT,f.getName(),f.getType());
            if (newV != null && !newV.equals(oldV) && StringUtils.isNotBlank(newV.toString())) {
                editMsg.append(annotationColumn.Desc() + " 从 " + oldV + "修改成" + newV + "<br>");
            }
        }
        return editMsg.toString();
    }

相关标签: java