记录对象修改的具体内容

创建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);
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值