Spring Data Jpa 配合MongoDB实现持久层对象属性动态增加

public interface StudentRepository extends MongoRepository<Student, Long> {
    Student findByName(String name);
}

MongoDB作为NOSQL数据库,基于文档存储这一特性,使得存储对象没有像关系型数据库有着约束。例如,当我们使用MySQL作为数据库,当我们想要增加持久层Entity属性时(所增加的属性,必须存储在数据库的情况,除非所增加的属性,不做为存储,只是持久层Entity临时的属性),不得不为这个Entity所对应的表,增加字段,否则所增加的属性无法存储,如果使用ORM框架,在映射过程中,还会出现异常。而MongoDB,使用BSON作为数据存储格式,使得其没有像关系型数据库这样较强的约束,在使用过程,持久层Entity可以随时动态增加属性,而MongoDB无需做任何更改。以下是Demo演示:


实体定义:

@AllArgsConstructor
@Data
@ToString
public class Student {
    @Id
    private Long id;

    private String name;
    private Integer age;
    private School shool;

    //第二次执行单元测试加的
//    private String home;

}
@AllArgsConstructor
@Data
@Builder
@ToString
public class School {
    @Id
    private Long id;

    private String name;
    private String address;

}


Repository:

public interface SchoolReponsitory extends MongoRepository<School,Long> {
    School findSchoolByName(String name);
}
public interface StudentRepository extends MongoRepository<Student, Long> {
    Student findByName(String name);
}

单元测试:

第一次单元测试,Student没有home属性

	/**
	 * 第一次单元测试
	 * - student实体没有加home属性
	 *
	 * @throws Exception
	 */
	@Test
	public void insertStudentWithoutHome() throws Exception {
		School school1 = schoolReponsitory.findSchoolByName("南京路中学");
		School school2 = schoolReponsitory.findSchoolByName("北京路中学");

		studentRepository.save(new Student(1L, "小明", 30,school1));
		studentRepository.save(new Student(2L, "小红", 40,school1));
		studentRepository.save(new Student(3L, "小王", 50,school2));
	}
第二次单元测试,Student有home属性

	/**
	 * 第二次单元测试
	 * - student实体加home属性
	 * @throws Exception
	 */
	@Test
	public void insertStudentWitHome() throws Exception {
		School school1 = schoolReponsitory.findSchoolByName("南京路中学");
		School school2 = schoolReponsitory.findSchoolByName("北京路中学");

		studentRepository.save(new Student(4L, "tom", 30,school1,"1小区"));
		studentRepository.save(new Student(5L, "peter", 40,school1,"2小区"));
		studentRepository.save(new Student(6L, "joy", 50,school2,"3小区"));
	}

结果:

第一次单元测试,MongoDB存储结果:

第二次单元测试MongoDB存储结果:


从mongodb存储结果发现:

第二次单元测试时,为Student加上了home属性,但是第一次存储的数据,并没有加上home属性,而且对mongodb没有做任何更改操作。


在对上面存储的数据,进行查询时,对Student中home属性,增加亦或是删除,对程序正常进行没有影响,以下是查询的单元测试代码:

        @Test
	public void findAll(){
		List<Student> students = studentRepository.findAll();
		students.forEach(student -> {
			System.out.println(student);
		});
	}
Student中没有home属性时,打印结果如下:

Student(id=1, name=小明, age=30, shool=School(id=1, name=南京路中学, address=南京路))
Student(id=2, name=小红, age=40, shool=School(id=1, name=南京路中学, address=南京路))
Student(id=3, name=小王, age=50, shool=School(id=2, name=北京路中学, address=北京路))
Student(id=4, name=tom, age=30, shool=School(id=1, name=南京路中学, address=南京路))
Student(id=5, name=peter, age=40, shool=School(id=1, name=南京路中学, address=南京路))
Student(id=6, name=joy, age=50, shool=School(id=2, name=北京路中学, address=北京路))
Student有home属性时,打印结果如下:

Student(id=1, name=小明, age=30, shool=School(id=1, name=南京路中学, address=南京路), home=null)
Student(id=2, name=小红, age=40, shool=School(id=1, name=南京路中学, address=南京路), home=null)
Student(id=3, name=小王, age=50, shool=School(id=2, name=北京路中学, address=北京路), home=null)
Student(id=4, name=tom, age=30, shool=School(id=1, name=南京路中学, address=南京路), home=1小区)
Student(id=5, name=peter, age=40, shool=School(id=1, name=南京路中学, address=南京路), home=2小区)
Student(id=6, name=joy, age=50, shool=School(id=2, name=北京路中学, address=北京路), home=3小区)
上面这两个测试说明,说明在持久层Entity属性的增加和删除,MongoDB无需做任何修改情况下,程序依然能够正常执行。



猜你喜欢

转载自blog.csdn.net/farYang/article/details/77601409