创建Util类:BeanChangeUtil
import com.uuf.modules.zsxt.aspect.PropertyMsg;
import org.apache.poi.ss.formula.functions.T;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
public class BeanChangeUtil<T> {
public String contrastObj(Object oldBean, Object newBean) {
// 创建字符串拼接对象
StringBuilder str = new StringBuilder();
// 转换为传入的泛型T
T pojo1 = (T) oldBean;
T pojo2 = (T) newBean;
// 通过反射获取类的Class对象
Class clazz = pojo1.getClass();
// 获取类型及字段属性
Field[] fields = clazz.getDeclaredFields();
// return jdk8Before(fields, pojo1, pojo2, str, clazz);
return lambdaChange(fields, pojo1, pojo2, str,clazz);
}
// jdk8 普通循环方式
public String foreachChange(Field[] fields, T pojo1, T pojo2, StringBuilder str, Class clazz) {
int i = 1;
try {
for (Field field : fields) {
if (field.isAnnotationPresent(PropertyMsg.class)) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
// 获取对应属性值
Method getMethod = pd.getReadMethod();
Object o1 = getMethod.invoke(pojo1);
Object o2 = getMethod.invoke(pojo2);
if (o1 == null || o2 == null) {
continue;
}
if (!o1.toString().equals(o2.toString())) {
str.append(i + "、" + field.getAnnotation(PropertyMsg.class).value() + ":" + "修改前=>" + o1 + ",修改后=>" + o2 + "\n");
i++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return str.toString();
}
// lambda表达式,表达式内部的变量都是final修饰,需要传入需要传入final类型的数组
public String lambdaChange(Field[] fields, T pojo1, T pojo2, StringBuilder str, Class clazz) {
final int[] i = {1};
Arrays.asList(fields).forEach(f -> {
if (f.isAnnotationPresent(PropertyMsg.class)) {
try {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
// 获取对应属性值
Method getMethod = pd.getReadMethod();
Object o1 = getMethod.invoke(pojo1);
Object o2 = getMethod.invoke(pojo2);
if (o1 == null || o2 == null) {
return;
}
if (!o1.toString().equals(o2.toString())) {
str.append(i[0] + "、" + f.getAnnotation(PropertyMsg.class).value() + ":" + "修改前=>" + o1 + "\t修改后=>" + o2 + "\n");
i[0]++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
return str.toString();
}
}
自定义注解:PropertyMsg
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface PropertyMsg {
String value();
}
测试对象:
import com.uuf.modules.zsxt.aspect.PropertyMsg;
public class TestPojo {
@PropertyMsg("姓名")
private String name;
@PropertyMsg("性别")
private String sex;
@PropertyMsg("年龄")
private Integer age;
public TestPojo() {
super();
}
public TestPojo(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
测试:
import org.apache.commons.lang.StringUtils;
public class Test {
public static void main(String[] args){
TestPojo u1 = new TestPojo("我是谁", "ok", 30);
TestPojo u2 = new TestPojo("我在哪", "no", 20);
BeanChangeUtil<TestPojo> t = new BeanChangeUtil<>();
String str = t.contrastObj(u1, u2);
if (StringUtils.isBlank(str)) {
System.out.println("未有改变");
} else {
System.out.println(str);
}
}
}