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"); }
|