Mongodb and spring Integration

Source: Internet
Author: User

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.

  1. <! -- Mongodb spring -->
  2. <Dependency>
  3. <GroupId>Org. springframework. data</GroupId>
  4. <ArtifactId>Spring-data-mongodb</ArtifactId>
  5. <Version>1.0.0.M5</Version>
  6. </Dependency>

Then the configuration file

  1. <?Xml Version="1.0" Encoding=UTF-8"?>
  2. <Beans Xmlns=Http://www.springframework.org/schema/beans"
  3. Xmlns: xsi=Http://www.w3.org/2001/XMLSchema-instance" Xmlns: context=Http://www.springframework.org/schema/context"
  4. Xmlns: mongo=Http://www.springframework.org/schema/data/mongo"
  5. Xsi: schemaLocation= "Http://www.springframework.org/schema/context
  6. Http://www.springframework.org/schema/context/spring-context-3.0.xsd
  7. Http://www.springframework.org/schema/data/mongo
  8. Http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
  9. Http://www.springframework.org/schema/beans
  10. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  11. <Context: property-placeholder Location="Classpath *: META-INF/mongodb. properties"/>
  12. <! -- 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. -->
  13. <Mongo: mongo Id="Mongo" Replica-set="Localhost: 27017">
  14. <! -- Set some connection Attributes -->
  15. <Mongo: options
  16. Connections-per-host="$ {Mongo. connectionsPerHost }"
  17. Threads-allowed-to-block-for-connection-multiplier="$ {Mongo. threadsAllowedToBlockForConnectionMultiplier }"
  18. Connect-timeout="$ {Mongo. connectTimeout }"
  19. Max-wait-time="$ {Mongo. maxWaitTime }"
  20. Auto-connect-retry="$ {Mongo. autoConnectRetry }"
  21. Socket-keep-alive="$ {Mongo. socketKeepAlive }"
  22. Socket-timeout="$ {Mongo. socketTimeout }"
  23. Slave-OK="$ {Mongo. slaveOk }"
  24. Write-number="1"
  25. Write-timeout="0"
  26. Write-fsync="True"/>
  27. </Mongo: mongo>
  28. <! -- 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. -->
  29. <Mongo: db-factory Dbname="Test" Mongo-ref="Mongo"/>
  30. <! -- The primary operation object of mongodb. All operations for adding, deleting, modifying, and querying mongodb are completed through it -->
  31. <Bean Id="Template" Class="Org. springframework. data. mongodb. core. Elastic template">
  32. <Constructor-arg Name="MongoDbFactory" Ref="MongoDbFactory"/>
  33. </Bean>
  34. <! -- Ing converter: scan the files in the back-package directory and use them as a collection ing of mongodb according to the annotations -->
  35. <Mongo: mapping-converter Base-package="Com. xxx. xxx. domain" />
  36. <! -- The repository directory of mongodb bean automatically scans the interface that extends the MongoRepository interface for injection. -->
  37. <Mongo: repositories Base-package="Com. xxx. xxx. persist. mongodb"/>
  38. <! -- To translate any exceptions thrown in @ Repository annotated classes -->
  39. <Context: annotation-config />
  40. </Beans>

In this way, the basic configuration is complete, and other in-depth operations will be described in subsequent articles.

  • 1
  • 2
  • 3
  • Next Page
[Content navigation]
Page 1: Configuration Page 1: Object ing
Page 1: MongoRepository for addition, deletion, modification, and query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.