Hug nosql--to talk about the simple use of spring Data MongoDB

Source: Internet
Author: User

Let's talk about what MongoDB is.

mongodb is a product between a relational database and a non-relational database, the most versatile of the non-relational databases, most like relational databases. The data structure he supports is very loose and is a JSON-like Bjson format, so you can store more complex data types. MONGO's biggest feature is that the query language he supports is very powerful, and its syntax is a bit like an object-oriented query language that almost implements most of the functionality of a relational database single-table query, and also supports indexing of data.     for collection storage, easy to store object type data. mode free. supports dynamic queries. supports full indexing and contains internal objects. support query. uses efficient binary data storage, including large objects such as video. automatically handles fragmentation to support scalability at the cloud level support ruby,python,java,c++, PHP and many other languages. file storage format Bson (an extension of JSON) network access

MongoDB website also has the API which the language uses, certainly also has the Java. But most of what is written on weekdays is based on the spring framework, and hopefully the operation of the database is under Spring's control. Just a few days ago Spring's official website saw Spring DATA MONGODB, interested in a little bit of a try.

The October 24 release is spring Data MongoDB 1.0.0.M5 released. Nature also needs the spring framework ha.

First, refer to the relevant package, mainly the following two:

Spring-data-mongodb-1.0.0.m4.jar

Spring-data-commons-1.2.0.m1.jar

Other things like Mongo-java-driver.jar need to see their own needs.

Let's start with the operation below.

1. First register an instance of MONGO

(1) Method one

@Configuration
public class AppConfig {
Public @Bean Mongo Mongo () throws Unknownhostexception {
return new Mongo ("localhost"); If it's remote, just fill in the IP
}
}
(2) Method Two: 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>
There are some details you can set:
<mongo:mongo host= "localhost" port= "27017" >
<mongo:options connections-per-host= "8"
Threads-allowed-to-block-for-connection-multiplier= "4"
connect-timeout= "+" //Connection time-out
Max-wait-time= "" //Wait Time
Auto-connect-retry= "true"

socket-timeout= "//socket" time-out period
Slave-ok= "true"
Write-number= "1"
write-timeout= "0"
Write-fsync= "true"/>
</mongo:mongo/>


To tell the truth, I am not very clear about some of the timeout points, I hope to know the advice of friends.
2.MongoDbFactory
This is located under the Org.springframework.data.mongodb.core, there are two main methods
DB Getdb () throws DataAccessException;
DB Getdb (String dbName) throws DataAccessException;
Register a factory instance now
(1) Method one
@Configuration
public class Mongoconfiguration {
Public @Bean mongodbfactory Mongodbfactory () throws Exception {
return new Simplemongodbfactory (new Mongo (), "databaseName");
}
}

(2) Method two: XML configuration

<mongo:db-factory dbname= "Database" >

There are also Username,password properties to choose from and refer to the documentation in detail.

Use of 3.MongoTemplate

or Register First:

Public @Bean mongotemplate Mongotemplate () throws Exception {
return new Mongotemplate (MONGO (), "databaseName");
}
also supports XML mode:
<bean id= "Mongotemplate" class= "Org.springframework.data.mongodb.core.MongoTemplate" >
<constructor-arg ref= "Mongo"/>
<constructor-arg name= "DatabaseName" value= "test"/>
</bean>
The configuration here is almost there.
There is nothing to say in the specific use, the reference API can be done.
Most commonly used:
Insert
Save
Remove
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)

Hug nosql--to talk about the simple use of spring Data MongoDB

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.