Let's talk about what MongoDB is.
MongoDB is a product between relational databases and non-relational databases. It has the most abundant functions and features like relational databases. The supported data structure is very loose and is similar to the JSON bjson format. Therefore, it can store complicated data types. The biggest feature of Mongo is that it supports a very powerful query language. Its syntax is somewhat similar to an Object-Oriented Query Language. It can almost implement most of the functions similar to single-table queries in relational databases, it also supports data indexing. It features high performance, ease of deployment, and ease of use, making it easy to store data. The main functions and features include: oriented to set storage and easy to store object-type data. Free mode. Supports dynamic query. Supports full indexing, including internal objects. Query is supported. Supports replication and fault recovery. Use efficient binary data storage, including large objects (such as videos ). Automatic fragment processing to support cloud computing scalability and support Ruby, Python, Java, C ++, PHP, and other languages. The file storage format is bson (a json extension) which can be accessed through the network.
The official MongoDB website also has APIs for various languages, and of course Java APIs. However, most of the things written on weekdays are based on the Spring framework. Naturally, we hope that the database operations will be under spring's control. I saw spring data MongoDB on the official spring website just a few days ago. I am very interested in this.
Spring data MongoDB 1.0.0.m5 released was released in October 24. Spring framework is also required.
First, reference the relevant packages, mainly including the following two:
Spring-data-mongodb-1.0.0.M4.jar
Spring-data-commons-1.2.0.M1.jar
Other such as mongo-java-driver.jar is still needed, depending on your needs.
Next we started the operation.
1. Register a mongo instance first
(1) method 1
@ Configuration
Public ClassAppconfig {
Public@ Bean Mongo ()ThrowsUnknownhostexception {
Return NewMongo ("localhost ");//For remote access, enter the IP address.
}
}
(2) Method 2: xml 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" >
< Mongo: Mongo Host = "Localhost" Port = "27017" />
</ Beans >
You can also set some details:
< Mongo: Mongo Host = "Localhost" Port = "27017" >
< Mongo: Options Connections-per-host = "8"
Threads-allowed-to-block-for-connection-multiplier = "4"
Connect-Timeout = "1000" // Connection timeout
Max-wait-time = "1500 }" // Wait time
Auto-connect-retry = "True"
Socket-keep-alive = "True"
Socket-Timeout = "1500" // Socket timeout
Slave-OK = "True"
Write-Number = "1"
Write-Timeout = "0"
Write-fsync = "True" />
</ Mongo: Mongo/ >
To be honest, I am not very clear about the timeout points. I hope you can give me some advice.
2. mongodbfactory
This is located under org. springframework. Data. MongoDB. Core. There are two main methods:
DB getdb ()ThrowsDataaccessexception;
DB getdb (string dbname)ThrowsDataaccessexception;
Register a factory instance
(1) method 1
@ Configuration
Public ClassConfiguration {
Public@ Bean mongodbfactory ()ThrowsException {
Return NewSimpledomaindbfactory (NewMongo (), "databasename ");
}
}
(2) Method 2: xml configuration
<Mongo: DB-factoryDbname= "Database">
The attributes of username and password are optional. For more information, see.
3. Use of Template
Register first:
Public@ Bean jsontemplate ()ThrowsException {
Return NewUsing template (Mongo (), "databasename ");
}
The XML method is also supported:
< Bean ID = "Template" Class = "Org. springframework. Data. MongoDB. Core. mongotemplate" >
< Constructor-Arg Ref = "Mongo" />
< Constructor-Arg Name = "Databasename" Value = "Test" />
</ Bean >
The configuration is almost done here.
There is nothing to say about the specific use. You can do it by referring to the API.
Most commonly used:
CreateInsert
SaveSave
DeleteRemove
Document address: http://static.springsource.org/spring-data/data-mongo/docs/1.0.0.M5/api/org/springframework/data/mongodb/core/MongoTemplate.html#MongoTemplate (Org. springframework. Data. MongoDB. mongodbfactory)