pom.xml jar 包管理 ,使用的是2.x版的monggoDrive
spring配置 spring-mongo.xml
<!-- mongodb配置单机 -->
<mongo:mongo id="mongo" host="127.0.0.1" port="27017" >
<mongo:options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1500"
max-wait-time="1500"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
write-number="1"
write-timeout="0" write-fsync="true"/>
</mongo:mongo>
<!-- 设置使用的数据库 名-->
<mongo:db-factory dbname="testdb" username="test" password="test" mongo-ref="mongo"/>
<!-- mongodb的模板 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
测试类方法
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
public class MongoSpringTest {
@Autowired
private MongoTemplate mongoTemplate;
// 使用spring整合的话, 就直接注入就可以了, 这是测试
@Before
public void testBefore() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context*.xml");
mongoTemplate = (MongoTemplate) context.getBean("mongoTemplate");
}
/**
* 插入用户信息
*/
@Test
public void testAddUser() {
long time1 = new Date().getTime();
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
for(int i = 0;i<100000;i++){
Map<String, Object> hashMap = new HashMap<String,Object>();
hashMap.put("id", ""+i);
hashMap.put("name", "zhouhuang");
list.add(hashMap);
if(i%10000 == 0){
mongoTemplate.insert(list, "zh");
list.removeAll(list);
}
}
long time2 = new Date().getTime();
System.out.println("-------------"+(time2-time1));
}
}
<!-- mongodb开始 -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.13.0-rc0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-cross-store</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-log4j</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!-- mongodb结束 -->
spring配置 spring-mongo.xml
<!-- mongodb配置单机 -->
<mongo:mongo id="mongo" host="127.0.0.1" port="27017" >
<mongo:options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1500"
max-wait-time="1500"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
write-number="1"
write-timeout="0" write-fsync="true"/>
</mongo:mongo>
<!-- 设置使用的数据库 名-->
<mongo:db-factory dbname="testdb" username="test" password="test" mongo-ref="mongo"/>
<!-- mongodb的模板 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
测试类方法
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
public class MongoSpringTest {
@Autowired
private MongoTemplate mongoTemplate;
// 使用spring整合的话, 就直接注入就可以了, 这是测试
@Before
public void testBefore() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context*.xml");
mongoTemplate = (MongoTemplate) context.getBean("mongoTemplate");
}
/**
* 插入用户信息
*/
@Test
public void testAddUser() {
long time1 = new Date().getTime();
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
for(int i = 0;i<100000;i++){
Map<String, Object> hashMap = new HashMap<String,Object>();
hashMap.put("id", ""+i);
hashMap.put("name", "zhouhuang");
list.add(hashMap);
if(i%10000 == 0){
mongoTemplate.insert(list, "zh");
list.removeAll(list);
}
}
long time2 = new Date().getTime();
System.out.println("-------------"+(time2-time1));
}
}