bean反射比较两个bean属性值的修改明细

2024-10-09 08:01:46 浏览数 (2)

1.期望:将【username】从'111'改成'222';将【address】从'这是一个测试数据'改成'这是一个真实数据';

2.导入jar

代码语言:javascript复制
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.21.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>

3.model类

代码语言:javascript复制
public class UserModel {
    private String username;
    private String sex;
    private String age;
    private String address;
    
    
    @Override
    public String toString() {
        return "UserModel [username="   username   ", sex="   sex   ", age="
                  age   ", address="   address   "]";
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
}

4.对比类及测试方法

代码语言:javascript复制
public class MyBeanCompareTest {

     public static void main(String[] args) throws Exception {
         UserModel oldData = new UserModel();
            oldData.setUsername("111");
            oldData.setAddress("这是一个测试数据");


            UserModel newData = new UserModel();
            newData.setUsername("222");
            newData.setAddress("这是一个真实数据");


            String desc = compare(oldData, newData);
            System.out.println(desc);
    }

    private static String compare(UserModel oldData, UserModel newData) throws Exception {
        StringBuffer content = new StringBuffer();
        
        Class clazz = newData.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // with modifiers "private"
            ReflectionUtils.makeAccessible(field);
            Object newValue = field.get(newData);
            System.out.print("fieldName=" field.getName() ",fieldValue=" String.valueOf(newValue));
                Object oldValue = field.get(oldData);
                System.out.println("tfieldName=" field.getName() ",fieldValue=" String.valueOf(oldValue));
                
                if(newValue != null && newValue != oldValue){
                    
                    content.append("将【");
                   content.append(field.getName());
                   content.append("】");
                   content.append("从'");
                   content.append(String.valueOf(newValue));
                   content.append("'改成'");
                   content.append(String.valueOf(oldValue));
                   content.append("';");
               
                }
        }
        
         
        return content.toString();
    }
     
}

0 人点赞