Spring mongodb整合
注意一个特殊的Jar包:
spring-core-4.0.5.RELEASE.jar
ObjenesisStd这个类是spring4.0以上才提供的。
版本不对就会抛出:
Caused by: java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
spring-data-mongodb-1.5.0.RELEASE.jar
这个jar之前我用1.6会抛出下面的异常:
org.springframework.data.mapping.model.FieldNamingStrategy
1.加入连接配置文件mongodb.properties:
mongo.host=127.0.0.1
mongo.port=27017
mongo.username=sa
mongo.password=sa
mongo.db=mydb
注意mongo.db的参数中带有空格或不符合数据名规范都会抛出错误。
2.加入spring配置文件applicationContext-mongodb.xml:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:cache = "http://www.springframework.org/schema/cache" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd "> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:mongodb.properties</value> </list> </property> </bean> <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}" /> <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> <constructor-arg name="username" value="${mongo.username}"/> <constructor-arg name="password" value="${mongo.password}"/> </bean> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg value="${mongo.db}" /> <constructor-arg ref="userCredentials" /> </bean> </beans>
3.创建MongoTemplate对象获得类:
package com.engine.manage.dao; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.data.mongodb.core.MongoTemplate; public abstract class AbstractBaseMongoTemplete implements ApplicationContextAware { protected MongoTemplate mongoTemplate; public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { MongoTemplate mongoTemplate = applicationContext.getBean("mongoTemplate", MongoTemplate.class); setMongoTemplate(mongoTemplate); } }
4.创建一个操作类,继承MongoTemplate对象获得类:
package com.engine.manage.dao; import org.springframework.stereotype.Repository; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; /** * mongoDB操作 * @author cglin-work * */ @Repository public class MongoDbDao extends AbstractBaseMongoTemplete{ private DBCollection dbCollection = null; private final String collection = "myCollection"; private DBCollection getConn(){ if(dbCollection == null){ return mongoTemplate.getCollection(collection); } return dbCollection; } public void save(BasicDBObject bdbObj) { getConn().save(bdbObj); } }
5.引用
@Autowired private MongoDbDao tdao; public MongoDbDao getTdao() { return tdao; } public void setTdao(MongoDbDao tdao) { this.tdao = tdao; } @Override public void saveTest(){ BasicDBObject bdbObj = new BasicDBObject(); bdbObj.put("a", "is a"); tdao.save(bdbObj); }