注意: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); } }
相关推荐
使用Spring Data MongoDB和MongoDB 3.0 17.1.1。配置选项 17.1.2。WriteConcern和WriteConcernChecking 17.1.3。认证 17.1.4。服务器端验证 17.1.5。其他事情要注意 附录 附录A:命名空间参考 元素 附录B:Poppers...
Spring Data MongoDB 的测试项目 在运行集成测试 (mvn verify) 之前。 确保您有一个在 localhost 上运行的 mongodb 实例。 它将使用本地主机上的测试数据库。 见 src/main/resources/application-test.properties
spring-boot2.0全新教程实例20例.zip - [spring-boot-helloWorld](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-helloWorld):Spring Boot 的 hello World 版本 - [spring-boot-...
主要介绍了springboot Mongodb的集成与使用实例详解,需要的朋友可以参考下
这是一个经典的SpringMvc学习实例,其中集成了hibernate,同时数据库采用了mysql 和 mongodb,在同一页面中同时显示两个数据访问结果。值得大家学习。
whatsmars-spring spring原理、集成及新特性 whatsmars-tomcat 模拟tomcat实现 whatsmars-dbrouter 分库分表实现 whatsmars-worker 多实例任务调度 whatsmars-dubbo Dubbo demo whatsmars-mq rocketmq,activemq ...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
whatsmars-spring spring原理、集成及新特性 whatsmars-tomcat 模拟tomcat实现 whatsmars-dbrouter 分库分表实现 whatsmars-worker 多实例任务调度 whatsmars-dubbo Dubbo demo whatsmars-mq rocketmq,activemq ...
AWS,MongoDB,Spring Framework,ETL 开发项目 项目源代码: 使用Bash开发了一个集群管理解决方案,以记录硬件规格并定期监视服务器集群中各个节点的资源使用情况.Docker使用Docker实例化RDBMS PostgreSQL数据库来...