Question about comparing attribute values of objects in collections

Question about comparing attribute values ​​of objects in collections

The landlord recently encountered a scene where there is a verification in the project. It is necessary to compare the collection object of the database query with the collection object passed by the front end to see whether the data has been modified.

1 Problem statement

Based on the above project requirements, the project is an older traditional project, without using the Lombok plug-in ( emphasis added ), entity classes, DTO, and BO objects are all original get and set methods, etc. The project was working fine before without any issues. When new requirements are received, some fields need to be added.

After another basic CRUD operation, the code is added, and there is no problem in adding, deleting, modifying and checking the pages corresponding to the entity class. So with extreme confidence, submit the code. (O(∩_∩)O)

At the above verification point in the code, an error was reported suddenly, and I was directly confused.

2 Troubleshooting

Based on the thinking that wherever there is a problem, it will be solved.

  • First of all, I suspect that there is an error in the front-end value transfer ( front-end problem ), add relevant logs at the collection comparison point, grab the logs, analyze and compare the data, and find that there is no problem with the data, whether it is front-end parameter transfer or back-end database query.

  • The problem is already obvious, there is a problem with the added code. ( Impossible, absolutely impossible ), of course, but it can be confirmed that the problem was added by myself. Then I looked at it, the method of collection comparison is collections.subtract(). This method is often used to compare the difference between two sets, and is generally used in basic data type comparisons, such as string sets, number sets, and so on. Compared with the object collection, it is still used less.

I already know that it is the problem of this method comparison. I checked the new fields of the entity class and DTO object. The set and get methods have also been written, including the toString method. After looking at the two objects, I lost my mind. I suddenly thought of comparing the values ​​of the two objects, and I need to rewrite the equals and hashcode methods. ( Pinch yourself ) Check the entity class and DTO object, because the entity class does not compare, so the equals and hashcode methods are not added. The DTO object is used for comparison, so these two methods are rewritten. After looking at the DTO object, only attributes and get, set methods and toString methods are added. According to the idea, add new fields to the equals and hashcode methods, start the project test, and it's a success! ( sprinkle flowers )

3 Summary and Pseudocode

After thinking about it, a problem arose.

Because I usually use the lombok plug-in when I come into contact with new projects, I only need to pay attention to the object itself and the sql spelling in the xml file when deleting the fields of the object. I don’t pay much attention to the basic toString, equals, hashcode and other methods of the object. ( Basically ignored ) In the old project, the lombok plug-in was not used, and no one came to help write these basic object methods, so when a simple increase or decrease of fields is required, it takes some time to pay attention to the methods of the object itself. This also shows that the lombok plugin is also quite convenient, reducing such small problems.

pseudocode

original object

public class UserDTO {
    
    

    private String id;
    
    private String name;

    public String getId() {
    
    
        return id;
    }

    public void setId(String id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "UserDTO{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserDTO userDTO = (UserDTO) o;
        return Objects.equals(id, userDTO.id) &&
                Objects.equals(name, userDTO.name);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(id, name);
    }
}

add new field

public class UserDTO {
    
    

    private String id;

    private String name;

    // 新加字段手机号
    private String phone;

    public String getPhone() {
    
    
        return phone;
    }

    public void setPhone(String phone) {
    
    
        this.phone = phone;
    }

    public String getId() {
    
    
        return id;
    }

    public void setId(String id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "UserDTO{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserDTO userDTO = (UserDTO) o;
        return Objects.equals(id, userDTO.id) &&
                Objects.equals(name, userDTO.name);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(id, name);
    }
}

compare logic code

// 前端传参数
List<UserDTO> reqList = new ArrayList();
// 数据库查询
List<UserDTO> respList = new ArrayList();
// 两个集合差集
List<UserDTO> resultList = CollectionUtils.subtract(reqList,respList)
// 判断集合中数据是否改变
if(CollectionUtils.isNotEmpty(resultList)){
    
    
    // 前端数据中,传了手机号,一直识别不出来,添加名称则可以识别出来,在DTO对象中equals方法中添加手机号即可
}

final state

public class UserDTO {
    
    

    private String id;

    private String name;

    private String phone;

	// 省略set/get方法

    @Override
    public String toString() {
    
    
        return "UserDTO{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserDTO userDTO = (UserDTO) o;
        return Objects.equals(id, userDTO.id) &&
                Objects.equals(name, userDTO.name) &&
                Objects.equals(phone, userDTO.phone);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(id, name, phone);
    }
}

Guess you like

Origin blog.csdn.net/ABestRookie/article/details/129017528