demo下载:test-mongodb.zip
mongodb服务器端权限配置参考上一篇《mongodb出窥及简单的权限配置》
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.7.2.RELEASE</version> </dependency> |
二、添加log4j.properties文件
1 2 3 4 5 6 |
<mongo:db-factory dbname="test_db" host="192.168.179.128" port="27017" username="rex" password="123456"/> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> </bean> |
1 2 3 4 5 6 7 |
@Document(collection="test_user") public class Customer { @Id private String id; ....... } |
Document属性标示映射的collection名称, @Id映射为"_id"字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:applicationContext.xml"); MongoTemplate template = applicationContext .getBean(MongoTemplate.class); long l = System.currentTimeMillis(); List<Customer> list = new ArrayList<Customer>(); for(int i = 0; i< 1000; i++) { list.clear(); for(int j =0 ; j< 1000; j ++) { list.add(new Customer("hml"+(i*j), "huang", j%100, new Date())); } template.insert(list, Customer.class); } System.out.println(System.currentTimeMillis() - l); applicationContext.close(); |
本博文由javacode.cn提供
Posted in: MongoDB
Comments are closed.