Spring4.0 整合MongoDB

測試spring整合mongodb,並批量插入數據demojava

1。pom管理並加載mongodb 整合入spring所需的jar包spring

<!-- 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結束 -->

2。spring配置mongodb數據源信息,並配置mongodb的模板templatemongodb

<!-- 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>

2.1 。若是是mongdb集羣,則將所有的ip配置入配置數據庫

<mongo:mongo id="mongo" replica-set="127.0.0.1:27017,127.0.0.1:27019,..." >
        <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>

3。測試代碼socket

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");
			  /*  mongoTemplate.save(hashMap,"zhou");
			    System.out.println(i);*/
				list.add(hashMap);
				if(i%10000 == 0){
					mongoTemplate.insert(list, "zh");
					list.removeAll(list);
				}
		}
		
		long time2 = new Date().getTime();
		System.out.println("-------------"+(time2-time1));
      
    }
}
相關文章
相關標籤/搜索