1, Environment configuration: MongoDB installation: 1), download address: http://www.mongodb.org/downloads 2), Installation Guide: http://docs.mongodb.org/manual/installation/ steps: create/data/MongoDB/data under the/data directory and decompress the/data/MongoDB/log directory: tar-zxvf mongodb-linux-x86_64-2.2.2.tar to enter the bin directory of MongoDB and execute the following command :. /mongod -- dbpath/data/MongoDB/data -- logpath/data/MongoDB/log/Mongo. log -- logappend2, spring data MongoDB package download address: http://s3.amazonaws.com/dist.springframework. Org/release/sdmongo/spring-data-mongo-1.1.0.RELEASE.zip spring data common jar package download: Address: Repository address: https://github.com/mongodb/developer-java-driver/downloads4.pdf jarpack: aopalliance-1.0.jar, asm-all-3.3.1.jar, cglib-2.2.2.jar, slf4j-api-1.7.1.jar, slf4j-log4j12-1.6.1.jar, common S-logging.jar, log4j-1.2.9.jar, Spring framework jar package, mongo-2.10.1.jar, spring data MongoDB jar package, spring data common jar package 5, create spring_mongodb Java project, import the above jar package. Spring manages MongoDB configuration in two ways: 1) Use the @ configuration annotation for configuration: here you need to create a configuration class and return a mongodbfactory object, such:
@Configurationpublic class AppConfig {public @Bean MongoOperations mongoTemplate(Mongo mongo) { MongoTemplate mongoTemplate = new MongoTemplate(mongo, "test"); return mongoTemplate; }/* * Factory bean that creates the Mongo instance */ public @Bean MongoFactoryBean mongo() { MongoFactoryBean mongo = new MongoFactoryBean(); mongo.setHost("localhost"); return mongo; }/* * Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes */ public @Bean PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() { return new PersistenceExceptionTranslationPostProcessor(); }}
2) Configure in XML format:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><context:component-scan base-package="com.springdb.mongodb"><context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/></context:component-scan><bean id="mongoTemplate"><constructor-arg name="mongo" ref="mongo"/><constructor-arg name="databaseName" value="test"/></bean><!-- Factory bean that creates the Mongo instance --><bean id="mongo"><property name="host" value="192.168.130.170"/><property name="port" value="27017"/></bean><!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes --><bean/></beans>
Instance address download: http://download.csdn.net/detail/weijonathan/5045391