Spring配置MongoDB

直接正题:

1. Spring Maven

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

2. Spring XML

注意:此处spring mongo schema配置是最新2.0的,网上有很多教程都是1.0的以及spring官网的文档也是1.0,自己搞了半天才慢慢摸索出来,如有问题请各位师兄指正

http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd

<?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:task="http://www.springframework.org/schema/task"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/data/mongo
	http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd
	http://www.springframework.org/schema/task
	http://www.springframework.org/schema/task/spring-task.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/properties/mongo.properties</value>
            </list>
        </property>
    </bean>

    <!-- Mongo db -->
    <!-- 如需多个逗号隔开 -->
    <mongo:mongo-client id="mongo" credentials="${mongo.user}:${mongo.pwd}@${mongo.defaultDBName}" host="${mongo.host}" port="${mongo.port}" >
        <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>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo"/>
        <constructor-arg name="databaseName" value="${mongo.defaultDBName}"/>
    </bean>

</beans>

properties 

#mongo.replicaSet=119.29.28.171:10010

mongo.user=username
mongo.pwd=password
mongo.host=ipAddr
mongo.port=10086

mongo.defaultDBName=默认dbName
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1000
mongo.maxWaitTime=1500
mongo.socketKeepAlive=true
mongo.socketTimeout=1500

 

以上,配置mongo与spring已完成,使用时直接注入MongoTemplate即可。

import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yuwan 719812998@qq.com
 * @date 2017/11/9
 * @comment
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:config/xml/spring-applicationContext-bean.xml"})
@WebAppConfiguration("src/main/resources")
public class MongoTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void insert(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Medvin");
        map.put("age", "17");
        map.put("gender", "boy");
        mongoTemplate.insert(map, "test");
        System.err.println("===> inserted.");
    }

    @Test
    public void find() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Medvin");
        MongoCursor<Document> mongoCursor = mongoTemplate.getCollection("test").find().iterator();
        while(mongoCursor.hasNext()) {
            System.err.println("==>"+ mongoCursor.next().toJson());
        }
    }

}

 

转载于:https://my.oschina.net/medvin/blog/1570501

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值