MongoDB 在 Spring的数据应用

Getting started with MongoDB and Spring Data | Jeroen Reijn

MongoDB 作为非关系数据库,和关系数据库一样,可以作为系统的Repository仓储实现,该文演示如何结合Spring进行数据管理,只要在模型数据上标记@Document,建立仓储PersonRepository,调用代码如下:


public static void main( String[] args ) {
logger.info("Bootstrapping MongoDemo application");

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
"META-INF/spring/applicationContext.xml");

PersonRepository personRepository = context.getBean(PersonRepository.class);

// cleanup person collection before insertion
personRepository.dropPersonCollection();

//create person collection
personRepository.createPersonCollection();

for(int i=0; i<20; i++) {
personRepository.insertPersonWithNameJohnAndRandomAge();
}

personRepository.logAllPersons();
logger.info(
"Finished MongoDemo application");
}

Spring搞了个Spring Data Document - MongoDB的东西,集成了MongoDB,主要在于和spring的结合,简化了通用的CRUD方式。

用起来满方便的。