spring3.2.4.RELEASE整合mongodb3.0

spring3.2.4.RELEASE整合mongodb3.0


一、添加pom配置

<dependency>  
          <groupId>org.springframework.datagroupId>  
          <artifactId>spring-data-mongodbartifactId>  
            
                  
                <version>1.4.2.RELEASEversion>  
                <exclusions>  
                    <exclusion>  
                        <groupId>org.mongodbgroupId>  
                        <artifactId>mongo-java-driverartifactId>  
                    exclusion>  
                exclusions>  
        dependency>  
          
        <dependency>  
            <groupId>org.mongodbgroupId>  
            <artifactId>mongo-java-driverartifactId>  
            <version>2.14.2version>  
        dependency>

二、添加配置文件


<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
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
    

    

     <context:property-placeholder location="classpath:config/mongodb.properties" />

     <mongo:mongo id="mongo" replica-set="${mongo.hostport}">
        <mongo:options connections-per-host="200"
            threads-allowed-to-block-for-connection-multiplier="100"
            connect-timeout="1000"
            max-wait-time="1500"
            auto-connect-retry="true"
            socket-keep-alive="true"
            socket-timeout="15000"
            slave-ok="true"
            write-number="1"
            write-timeout="0"
            write-fsync="true" />
    mongo:mongo>

    <mongo:db-factory dbname="database" mongo-ref="mongo" />
    
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo" />
        <constructor-arg name="databaseName" value="sms_platform2" />
    bean>

beans>

#mongo.host=192.168.1.176
#mongo.port=27017
#
#mongo.hostport=192.168.1.176:27017

mongo.host=localhost
mongo.port=27017

mongo.hostport=localhost:27017

三、将配置文件导入spring,使用spring进行启动

<import resource="classpath:config/spring-data-mongodb.xml"/>

你可能感兴趣的:(数据库)