Spring integrated mongodb instance

Note: The version of spring-data-mongodb is 1.9.2, and the version of spring 4.0 is required. If the version is lower than 4.0 at this time, an error will be reported during startup.

The error message is as follows:

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

 

 

 

Now start integrating mongo

 

 1. Using maven, the dependencies required by the pom

 


1. Configure maven dependencies
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.9.2.RELEASE</version></dependency>



2. mongodb configuration () is imported in the existing configuration file
<!--导入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);
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326309208&siteId=291194637