1.
反射比较两个对象属性值是否改变,并将哪个字段,字段名称,改之前的值,改之后的值入库
话不多说
先上才艺:
工具类代码如下:
/**
* @Description: 反射获取 两个对象之间的不同属性值
* @Param: old 数据库里对象 , last 前段传过来的对象
* @Date:
*/
public static void compareObjects(Object old , Object last) throws IllegalAccessException {
List<Log> logs = new ArrayList<>();
if (old.getClass() != last.getClass()) {
throw new RuntimeException("不是同一类型的对象不比较!!");
}
Class clazz = old.getClass();
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
// 只比较字符串和 int 类型
Class<?> type = field.getType();
if (type == String.class
|| type == Integer.class
){
Object oldValueObj = field.get(old);
Object lastValueObj = field.get(last);
// 新旧数据都不为 null的时候 打印数据
if (null != oldValueObj && null != lastValueObj ) {
// 比较相等
if (!lastValueObj.toString().equals(oldValueObj.toString())) {
System.out.println(oldValueObj + " " + lastValueObj );
// 读取字段名称
String column = field.getName();
// 读取属性的汉字名称
AttributeName annotation = field.getAnnotation(AttributeName.class);
String name = annotation.value();
// 表里状态的处理
if (field.isAnnotationPresent(ValidAttribute.class) && field.getAnnotation(ValidAttribute.class).isDeleted() != (Integer) lastValueObj) {
oldValueObj = "启用";
lastValueObj = "删除";
}
logs.add(new Log(oldValueObj.toString(),lastValueObj.toString(),name,column));
}
}
}
}
// 遍历 logs
logs.forEach(log ->{
System.out.println(log.toString());
});
测试代码如下:
public static void main(String[] args) throws IllegalAccessException {
Region old = new Region();
old.setId(1);
old.setCode("001");
old.setName("北京");
old.setDescription("这是条旧的数据");
old.setParentCode("0");
old.setIsDeteted(0);
Region last = new Region();
last.setId(1);
last.setCode("001");
last.setName("北京市");
last.setDescription("这是修改后的数据");
last.setParentCode("0");
last.setIsDeteted(1);
// 调用比较
compareObjects(old,last);
}
1. 用到的两个注解:
package com.example.demo.entity.annotation;
import java.lang.annotation.*;
/**
* @Description: 获取汉字名称的注解
* @Param:
* @return:
* @Author: guoyiguang
* @Date:
*/
@Documented
@Target(ElementType.FIELD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface AttributeName {
String value() default "";
}
package com.example.demo.entity.annotation;
import java.lang.annotation.*;
/**
* @Description: 针对库里是数字,要在前台显示 对应数字的 字段
* @Author: guoyiguang
* @Date:
*/
@Documented
@Target(ElementType.FIELD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidAttribute {
int isDeleted() default 0;
int isValid() default 1;
String comments() default "数字转汉字注解";
}
用到的实体类:
package com.example.demo.entity;
import com.example.demo.entity.annotation.AttributeName;
import com.example.demo.entity.annotation.ValidAttribute;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
/**
* @program: springboot_01
* @description:
* @author: guoyiguang
* @create: 2021-01-14 14:43
**/
public class Region {
private Integer id;
@AttributeName("区域名称")
private String name;
private String code;
private String parentCode;
public List<String> getSonNameList() {
return sonNameList;
}
public void setSonNameList(List<String> sonNameList) {
this.sonNameList = sonNameList;
}
@AttributeName("区域描述信息")
private String description;
// -------------------------------以下为 测试反射 字段------------------------
private LocalDateTime createdTime;
private String creator;
/**
* 是否有效(测试数字转汉字)
*/
@AttributeName("是否有效")
@ValidAttribute(isDeleted = 0)
private Integer isDeteted;
private List<String> sonNameList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDateTime getCreatedTime() {
return createdTime;
}
public String getCreator() {
return creator;
}
public void setCreatedTime(LocalDateTime createdTime) {
this.createdTime = createdTime;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Integer getIsDeteted() {
return isDeteted;
}
public void setIsDeteted(Integer isDeteted) {
this.isDeteted = isDeteted;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Region region = (Region) o;
return Objects.equals(id, region.id) &&
Objects.equals(name, region.name) &&
Objects.equals(code, region.code) &&
Objects.equals(parentCode, region.parentCode) &&
Objects.equals(description, region.description);
}
@Override
public int hashCode() {
return Objects.hash(id, name, code, parentCode, description);
}
@Override
public String toString() {
return "Region{" +
"id=" + id +
", name='" + name + '\'' +
", code='" + code + '\'' +
", parentCode='" + parentCode + '\'' +
", description='" + description + '\'' +
'}';
}
}
package com.example.demo.entity;
/**
* @program: springboot_01
* @description: 日志记录
* @author: guoyiguang
* @create: 2021-02-21 09:38
**/
public class Log {
// 属性名称
private String name;
private String column;
private String oldValue;
private String lastValue;
public Log() {}
public Log(String oldValue, String lastValue, String name,String column) {
this.name = name;
this.column = column;
this.oldValue = oldValue;
this.lastValue = lastValue;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColumn() {
return column;
}
public void setColumn(String column) {
this.column = column;
}
public String getOldValue() {
return oldValue;
}
public void setOldValue(String oldValue) {
this.oldValue = oldValue;
}
public String getLastValue() {
return lastValue;
}
public void setLastValue(String lastValue) {
this.lastValue = lastValue;
}
@Override
public String toString() {
return "Log{" +
"name='" + name + '\'' +
", column='" + column + '\'' +
", oldValue='" + oldValue + '\'' +
", lastValue='" + lastValue + '\'' +
'}';
}
}