Before we start, please download and run MongoDB for your operating system. It's very simple so I won't spend time on this and let's start with simple POM file for our project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelversion >4.0.0</ modelversion > < groupid >mongodb</ groupid > < artifactid >com.example.spring</ artifactid > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < properties > < project.build.sourceencoding >UTF-8</ project.build.sourceencoding > < spring.version >3.0.5.RELEASE</ spring.version > </ properties > < dependencies > < dependency > < groupid >org.springframework.data</ groupid > < artifactid >spring-data-mongodb</ artifactid > < version >1.0.0.M3</ version > </ dependency > < dependency > < groupid >log4j</ groupid > < artifactid >log4j</ artifactid > < version >1.2.16</ version > </ dependency > < dependency > < groupid >org.mongodb</ groupid > < artifactid >mongo-java-driver</ artifactid > < version >2.5.3</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-core</ artifactid > < version >${spring.version}</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-context</ artifactid > < version >${spring.version}</ version > </ dependency > </ dependencies > < repositories > < repository > < id >springsource-milestone</ id > < name >Spring Framework Milestone Repository</ name > </ repository > </ repositories > </ project > |
- MongoDB java driver
- Spring Data for MongoDB
There are few ways to define MongoDB inside your Spring application context. Let me show a bit verbose but more flexible one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | < 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=" < context:annotation-config ></ context:annotation-config > < context:component-scan base-package = "com.example.mongodb" ></ context:component-scan > < mongo:jmx ></ mongo:jmx > < bean id = "mongoDbFactory" class = "org.springframework.data.document.mongodb.SimpleMongoDbFactory" > < constructor-arg index = "0" ref = "mongo" /> < constructor-arg index = "1" value = "elements-db" /> </ bean > < bean id = "mappingContext" class = "org.springframework.data.document.mongodb.mapping.MongoMappingContext" > </ bean > < bean id = "converter" class = "org.springframework.data.document.mongodb.convert.MappingMongoConverter" > < constructor-arg name = "mongoDbFactory" ref = "mongoDbFactory" /> < constructor-arg name = "mappingContext" ref = "mappingContext" /> </ bean > < bean id = "mongoTemplate" class = "org.springframework.data.document.mongodb.MongoTemplate" > < constructor-arg name = "mongoDbFactory" ref = "mongoDbFactory" /> < constructor-arg name = "mongoConverter" ref = "converter" /> < property name = "writeResultChecking" value = "EXCEPTION" /> < property name = "writeConcern" value = "NORMAL" /> </ bean > < bean id = "mongo" class = "org.springframework.data.document.mongodb.MongoFactoryBean" > < property name = "host" value = "localhost" ></ property > </ bean > </ beans > |
- mongo defines connection to MongoDB database (we rely on default settings, port 27027)
- converter is used to convert Java classes to/from MongoDB's DBObject (== JSON)
- mongoTemplate exposes operations we can do over MongoDB
So, we are ready to go!
Here are few code snippets to start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.example.mongodb; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.document.mongodb.CollectionCallback; import org.springframework.data.document.mongodb.MongoOperations; import org.springframework.data.document.mongodb.query.Index; import org.springframework.data.document.mongodb.query.Index.Duplicates; import org.springframework.data.document.mongodb.query.Order; import org.springframework.stereotype.Service; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.MongoException; @Service public class MongoService { @Autowired private MongoOperations template; public void createCollection( final String name ) { template.createCollection( name ); } public void dropCollection( final String name ) { template.dropCollection( name ); } public void insert( final Object object, final String collection ) { template.insert( object, collection ); } public void createIndex( final String name, final String collection ) { template.ensureIndex( new Index() .on( name, Order.DESCENDING ) .unique( Duplicates.DROP ), collection ); } // Remove / save / ... operations here } |
No comments:
Post a Comment