Here we use a project integrating MongoDB in spring-Data. First, add the dependency on it in maven. Here we use version 1.0.0.m5.
<!-- mongodb spring --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId><version>1.0.0.M5</version></dependency>
Then the configuration file
<? 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" xmlns: Mongo = "http://www.springframework.org/schema/data/mongo" xsi: schemalocation = "http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/ SC Hema/data/container http: // container http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <context: Property-placeholder location =" classpath *: META-INF/MongoDB. properties "/> <! -- Define the Mongo object, which corresponds to Mongo in the MongoDB official jar package. Replica-set sets the IP address and port of the cluster copy --> <Mongo: mongo id = "mongo" Replica-set = "localhost: 27017"> <! -- Set some connection Attributes --> <Mongo: Options connections-per-host = "$ {Mongo. connectionsperhost} "threads-allowed-to-block-for-connection-multiplier =" $ {Mongo. threadsallowedtoblockforconnectionmultiplier} "Connect-Timeout =" $ {Mongo. connecttimeout} "Max-wait-time =" $ {Mongo. maxwaittime} "auto-connect-retry =" $ {Mongo. autoconnectretry} "socket-keep-alive =" $ {Mongo. socketkeepalive} "socket-Timeout =" $ {Mongo. sockettimeout} "s Lave-OK = "$ {Mongo. slaveok} "Write-number =" 1 "Write-Timeout =" 0 "Write-fsync =" true "/> </Mongo: Mongo> <! -- Mongo factory, which is used to obtain the Mongo instance. dbname is the name of the MongoDB database. If not, it will be automatically created --> <Mongo: DB-factory dbname = "test" mongo-ref = "mongo"/> <! -- MongoDB's main operation object. All operations for adding, deleting, modifying, and querying MongoDB are completed through it --> <bean id = "mongotemplate" class = "org. springframework. data. mongoDB. core. using template "> <constructor-Arg name =" mongodbfactory "ref =" mongodbfactory "/> </bean> <! -- Ing converter: scan the files in the back-package directory and use them as a collection ing of MongoDB according to the annotations --> <Mongo: mapping-converter base-package = "com. xxx. xxx. domain "/> <! -- The repository directory of MongoDB bean automatically scans the interface that extends the mongorepository interface for injection --> <Mongo: repositories base-package = "com. xxx. xxx. persist. mongoDB "/> <! -- To translate any exceptions thrown in @ repository annotated classes --> <context: annotation-config/> </beans>
In this way, the basic configuration is complete, and other in-depth operations will be described in subsequent articles.