公司业务需求准备引入mongodb,mongodb在2014年就已经出了稳定版本,目前最新版本为3.4.
mac 安装mongo很简单 1.brew update 2.brew install mongodb
目前采用springmvc框架实现增删改查,spring-data 项目中又对mongodb做支持。
- Spring Data MongoDB - Spring based, object-document support and repositories for MongoDB.
引入mongodb的配置
pom引入jar
<!-- mongoDB --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.10.0.RELEASE</version> </dependency>
mongod.properties
#mongo连接地址和端口逗号分隔127.0.0.1:27017,localhost:27018 mongo.hostport=10.8.1.222: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.dbname=test
mongodb-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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="http://www.springframework.org/schema/context 3 http://www.springframework.org/schema/context/spring-context-3.0.xsd 4 http://www.springframework.org/schema/data/mongo 5 http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 8 9 <!-- 加载mongodb的属性配置文件 --> 10 <!-- <context:property-placeholder location="classpath:config/properties/mongodb.properties" /> --> 11 <!-- <context:property-placeholder location="classpath:mongodb.properties" /> 12 --> 13 <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 --> 14 <mongo:mongo id="mongo" replica-set="${mongo.hostport}"> 15 <!-- 一些连接属性的设置 --> 16 <mongo:options 17 connections-per-host="${mongo.connectionsPerHost}" 18 threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" 19 connect-timeout="${mongo.connectTimeout}" 20 max-wait-time="${mongo.maxWaitTime}" 21 auto-connect-retry="${mongo.autoConnectRetry}" 22 socket-keep-alive="${mongo.socketKeepAlive}" 23 socket-timeout="${mongo.socketTimeout}" 24 slave-ok="${mongo.slaveOk}" 25 write-number="1" 26 write-timeout="0" 27 write-fsync="true" /> 28 </mongo:mongo> 29 <mongo:db-factory 30 dbname="${mongo.dbname}" 31 mongo-ref="mongo" 32 password="${mongo.password}" 33 username="${mongo.username}" 34 /> 35 36 37 <bean id="mongoTemplate" name="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 38 <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> 39 </bean> 40 41 </beans>
如果不提供封装类,直接注入mongoTemplate即可。
@AutoWire
private MongoTemplate mongoTemplate;
简单的增删改查JunitTest:
package com.dooioo.led; import java.util.List; import org.junit.Test; 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; public class MongoTest extends BaseTest{ @Autowired private MongoTemplate mongoTemplate; @Test public void testQuery(){ List<testModel> findOne = mongoTemplate.find(new Query(Criteria.where("name").is("张文豪")), testModel.class); for(testModel t: findOne){ System.out.println(t.toString()); } } @Test public void testInsert() { testModel model = new testModel(); model.setName("张文豪"); model.setAge(10); model.setSex("男"); mongoTemplate.insert(model); /*try { MongoTemplate tmp = m.mongoTemplate(); //增 //m.mongoTemplate().insert(model); //查询 List<testModel> list = tmp.find(new Query(Criteria.where("name").is("张文豪")), testModel.class); for(testModel t: list){ System.out.println(t.toString()); } //更新 tmp.updateFirst(new Query(Criteria.where("name").is("张文豪")), new Update().update("sex","女"), testModel.class); //删除 WriteResult result = tmp.remove(new Query(Criteria.where("sex").is("女")),testModel.class); System.out.println(result.getN()); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } } class testModel{ private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "testModel [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
建议先了解mongodb 基本数据类型,数据存储模型,再围绕效率和索引进行学习。