之前做的一个微信端的项目:项目有一个需求是修改数据的时候,将进行修改了的数据记录下来,然后修改了多少数据加一定的积分。最近做了个项目:项目的需求是对一张单子的某几项进行修改,然后和修改之前的单子进行比较,展示修改了哪些内容,这两张单子都是可以在数据库不同表查出来的,但是这两张表的数据结构是相同的。代码是这样写的:第一个是主要方法,第二个是部分实体类。其实主要就是运用了反射。
public List<EndorseValue> queryDiffByID(V_FreightPolicy freightPolicy, V_FreightPolicy freightPolicyLong) throws Exception {
LOG.info(BaseLogInfo.formatLOGINFO("111", "queryDiffByID", "初始化"));
List list = new ArrayList<EndorseValue>();
List listDiff = new ArrayList<EndorseValue>();
Class clazz = Class.forName("com.ccsee.ecargo.model.db.V_FreightPolicy");
// 获取该类的所有属性字段V_FreightPolicy
Field[] fields = clazz.getDeclaredFields();
// 解析字段注解
for (Field field : fields) {
if (field.isAnnotationPresent(AnnotationField.class)) {
AnnotationField annoField = field.getAnnotation(AnnotationField.class);
EndorseValue keyValue = new EndorseValue();
keyValue.setKeyName(annoField.fieldValue());
keyValue.setKey(field.getName().toUpperCase());
list.add(keyValue);
}
}
Map<String, String> map = new HashMap<String, String>();
Method[] methods = clazz.getMethods();// 类的方法
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("get") && !methodName.equals("getClass")) {// 如果方法名以get开头
Object value = method.invoke(freightPolicy);// 调用方法,并打印返回值
if (value != null) {
map.put(methodName.substring(3).toUpperCase().toString(), value.toString());
}
}
}
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("get") && !methodName.equals("getClass")) {// 如果方法名以get开头
Object value = method.invoke(freightPolicyLong);// 调用方法,并打印返回值
value = (value != null ? value : "");
String me = methodName.substring(3).toUpperCase().toString();
for (int i = 0; i < list.size(); i++) {
EndorseValue key = (EndorseValue) list.get(i);
if (key.getKey().equals(me)) {
if (map.get(me) != null && !map.get(me).equals(value.toString())) {
key.setKey(me);
key.setValue(map.get(me));
key.setOldValue(value.toString());
listDiff.add(key);
}
}
}
}
}
return listDiff;
}
@AnnotationField(fieldValue = "额")
private String insureount = "0.00";
@AnnotationField(fieldValue = "约")
private String remark;
@AnnotationField(fieldValue = "赔")
private String deductible;
private String billNo;
private String billType;
private String glausesID;
private String insureName;
private String statusName;
private String agentLocID = "-1";
@AnnotationField(fieldValue = "地码")
private String agentLocExtno;
private String agentLocDesc;
private String transportTypeID;
@AnnotationField(fieldValue = "方式")
private String transportType;
@AnnotationField(fieldValue = "币")
private String currency;
@AnnotationField(fieldValue = "文称")
private String endCurrency;
@AnnotationField(fieldValue = "称")
private String currencyEn;
private String endTypeID;
@AnnotationField(fieldValue = "结称")
private String endType;
@AnnotationField(fieldValue = "发额")
private String invoiceAmount;
@AnnotationField(fieldValue = "发头")
private String invHead;
private String ifCharge;
private String ifBalance;
@AnnotationField(fieldValue = "投式")
private String sourceType;
@AnnotationField(fieldValue = "价件")
private String addPropTypeId;
private String addPropTypeID;
@AnnotationField(fieldValue = "价格")
private String addPropTypeValue;
@AnnotationField(fieldValue = "比例")
private String addProportion;
private String portId;
@AnnotationField(fieldValue = "费")
private String ratio;
@AnnotationField(fieldValue = "地")
private String paymentLoc = "";
private String totalAmountInsured;
private String policyNum;
private String policyNum2;
private String freightPolicyLongID = "-1";
private String creditid;
@AnnotationField(fieldValue = "号码")
private String creditId;
@AnnotationField(fieldValue = "份数")
private String printPolicyNum = "2";
private String changeDesc;
private String createId;
/**
* 重量
*/
@AnnotationField(fieldValue = "重量")
private String weights;
@AnnotationField(fieldValue = "PPP")
private String way;
private String selfaudit;
private String reinsuranceFlag;
private String usrDesc;
private String createTime;
private String clientName;
private String specialCode;
private String checkpolicystatusid;
private String checkpolicystatus;
@AnnotationField(fieldValue = "NNN")
private String consigneeInfo;
@AnnotationField(fieldValue = "LLL")
private String neijian;
private String payDate;
@AnnotationField(fieldValue = "MMM")
private String documentType;
private String holderadminflag;
private String recognizeeadminflag;
private String status;
public String getHolderadminflag() {
return holderadminflag;
}
public void setHolderadminflag(String holderadminflag) {
this.holderadminflag = holderadminflag;
}
public String getRecognizeeadminflag() {
return recognizeeadminflag;
}
public void setRecognizeeadminflag(String recognizeeadminflag) {
this.recognizeeadminflag = recognizeeadminflag;
}
public void setRatio(String ratio) {
this.ratio = ratio;
}
public String getCurrencyCHName() {
return CurrencySystem.getNameCn(getCurrencyID());
}
public String getPaymentLoc() {
return paymentLoc;
}
public void setPaymentLoc(String paymentLoc) {
this.paymentLoc = paymentLoc;
}
public String getTotalAmountInsured() {
return totalAmountInsured;
}
public void setTotalAmountInsured(String totalAmountInsured) {
this.totalAmountInsured = totalAmountInsured;
}
public String getPolicyNum() {
return policyNum;
}
public void setPolicyNum(String policyNum) {
this.policyNum = policyNum;
}
public String getPolicyNum2() {
return policyNum2;
}
public void setPolicyNum2(String policyNum2) {
this.policyNum2 = policyNum2;
}
public void setPrintPolicyNum(String printPolicyNum) {
this.printPolicyNum = printPolicyNum;
}
public String getChangeDesc() {
return changeDesc;
}
public void setChangeDesc(String changeDesc) {
this.changeDesc = changeDesc;
}
public String getCreateId() {
return createId;
}
public void setCreateId(String createId) {
this.createId = createId;
}
public String getWeights() {
return weights;
}
public void setWeights(String weights) {
this.weights = weights;
}
public String getWay() {
return way;
}
public void setWay(String way) {
this.way = way;
}
public String getTypeFlag() {
return typeFlag;
}
public void setTypeFlag(String typeFlag) {
this.typeFlag = typeFlag;
}
public String getPostalModeID() {
return postalModeID;
}
public void setPostalModeID(String postalModeID) {
this.postalModeID = postalModeID;
}
public String getBetweenYear() {
return betweenYear;
}
public void setBetweenYear(String betweenYear) {
this.betweenYear = betweenYear;
}
public String getShipflag() {
return shipflag;
}
}