注意:spring-data-mongodb的版本在1.9.2,需要spring4.0的版本,如果此时版本低于4.0,启动时会报错,
报错信息如下:
springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.mongodb.core.MongoTemplate com.iflytek.ecss.service.MongoService.mongoTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in URL [file:/F:/wokeplace/mongotest/target/mongotest/WEB-INF/classes/applicationContext-mongodb.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
现在开始集成mongo
1、使用maven,的pom需要的依赖
1、配置maven依赖 <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.9.2.RELEASE</version></dependency> 2、mongodb配置()在已有的配置文件里导入
<!--导入Mongo-> <import resource="applicationContext-mongodb.xml"/>
下面就是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:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!-- mongo对象 --> <mongo:mongo-client id="mongo" replica-set="${mongo.hostport}"> <mongo:client-options connections-per-host="${mongo.connectionsPerHost}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" /> </mongo:mongo-client> <mongo:db-factory dbname="database" mongo-ref="mongo" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="${mongo.databaseName}" /> </bean> </beans> 属性实例: application.properties 文件
###mongo mongo.hostport=127.0.0.1:27017 mongo.connectionsPerHost=8 mongo.threadsAllowedToBlockForConnectionMultiplier=4 #连接超时时间 mongo.connectTimeout=1000 #等待时间 mongo.maxWaitTime=1500 mongo.autoConnectRetry=true mongo.socketKeepAlive=true #Socket超时时间 mongo.socketTimeout=1500 mongo.slaveOk=true mongo.databaseName=userinfo
实例代码:
package com.iflytek.ecss.service; import com.iflytek.ecss.model.UserModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Service; import java.util.List; /** * Created by ss on 2017/4/18. */ @Service public class MongoService { @Autowired private MongoTemplate mongoTemplate; public void insert(UserModel userModel) { mongoTemplate.save(userModel); } public UserModel findModel(String openid) { Query query = Query.query(Criteria.where("openId").is(openid)); UserModel userModel = mongoTemplate.findOne(query, UserModel.class); return userModel; } public List<UserModel> findModelList(String telNo) { //精确查询 Query query = Query.query(Criteria.where("telNo").is(telNo)); //针对数组里进行and查询 Query query1 = Query.query(Criteria.where("adress").all("aaaa", "bbbb")); //正则匹配 Query query2 = Query.query(Criteria.where("adress").regex("nihao")); List<UserModel> userModels = mongoTemplate.find(query, UserModel.class); return userModels; } //更新 public void upset() { Query query = Query.query(Criteria.where("openId").is("7ccc61dd-bd79-4aa8-baa3-30f7845e8b11")); Update update = Update.update("nickName", "tom"); //只更新第一条 mongoTemplate.updateFirst(query, update, UserModel.class); //更新多条 mongoTemplate.updateMulti(query, update, UserModel.class); //有则更新,没有则插入 mongoTemplate.upsert(query, update, UserModel.class); } //删除 public void del() { Query query = Query.query(Criteria.where("openId").is("7ccc61dd-bd79-4aa8-baa3-30f7845e8b11")); mongoTemplate.remove(query, UserModel.class); } }