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

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();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值