Java中如何判断两个对象的属性是否一致(方法1)
需求:前端编辑对象后,将对象传给后端.后端需要判断是否修改了某一个属性.比如前端传过来的对象只修改了description属性,那么我就不算他修改.后端不做更新或做其他处理.
1.依赖
<dependency>
<groupId>com.github.colincatsu</groupId>
<artifactId>fast-object-diff</artifactId>
<version>1.1</version>
</dependency>
2.实体类
// User类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@DiffLog(name = "id")
private int id;
@DiffLog(name = "name")
private String name;
@DiffLog(name = "age")
private int age;
@DiffLog(name = "students")
private List<Student> students;
}
// Student类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
@DiffLogKey(name = "files id")
@DiffLog(name = "other")
private int id;
@DiffLog(name = "other")
private String username;
@DiffLog(name = "other")
private String password;
}
// 测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class FastObjectDiffTest {
@Test
public void test1() throws Exception {
Student student = new Student(1,"天数","123456");
List<Student> students = new ArrayList<>();
students.add(student);
Student student2 = new Student(1,"天数","666666");
List<Student> students2 = new ArrayList<>();
students2.add(student2);
User user1 = new User(1,"zhangsan",1,students);
User user2 = new User(2,"lisi",2,students2);
List<DiffWapper> diffWappers = AbstractObjectDiff.generateDiff(user1, user2);
System.out.println(diffWappers.size());
}
}