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/context
- Http://www.springframework.org/schema/context/spring-context-3.0.xsd
- Http://www.springframework.org/schema/data/mongo
- Http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
- Http://www.springframework.org/schema/beans
- 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 official mongodb 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 }"
- Slave-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"/>
- <! -- The primary operation object of mongodb. All operations for adding, deleting, modifying, and querying mongodb are completed through it -->
- <Bean Id="Template" Class="Org. springframework. data. mongodb. core. Elastic 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.
[Content navigation] |
Page 1: Configuration |
Page 1: Object ing |
Page 1: MongoRepository for addition, deletion, modification, and query |
|