MongoDB2.x升级到3.x解决方案

2018-12-19 01:44:47来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

MongoDB2.x版本Maven配置

<!-- mongodb -->
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>1.6.1.RELEASE</version>
</dependency>    
<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>2.12.4</version>
</dependency>
<dependency>
  <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-commons</artifactId>
    <version>1.9.1.RELEASE</version>
</dependency>

spring-mongo配置

<?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: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-3.0.xsd">
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="${mongo.databa.sename}" />
        <constructor-arg name="userCredentials" ref="mongoCredentials" />
    </bean>
    <bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials">
        <constructor-arg name="username" value="" />
        <constructor-arg name="password" value="" />
    </bean>
    <mongo:mongo host="${mongo.host}" port="${mongo.port}">
        <mongo:options connections-per-host="10" threads-allowed-to-block-for-connection-multiplier="100" connect-timeout="10000" max-wait-time="15000" auto-connect-retry="true" />
    </mongo:mongo>
    <bean id="mongoDao" class="com.tofba.dao.common.base.MongoDao">
        <property name="mongoTemplate" ref="mongoTemplate" />
    </bean>
</beans>

MongoDB3.x版本Maven配置

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

1.8.0版本自动依赖了mongo-java-driver,spring-data-commons无需导入其他Jar包

xml配置,配置如下:注意需要将spring组件升级到4.1.8以上要不然会报错哦!!!!!!!!!!!!!!!!!!!!!

<?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: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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <mongo:mongo id="mongo" replica-set="${mongo.hostport}"> <!-- 一些连接属性的设置 --> <mongo:options connections-per-host="${mongo.connectionsPerHost}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}" write-number="1" write-timeout="0" write-fsync="true" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" /> </mongo:mongo> <mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongo" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="${mongo.dbname}" /> <constructor-arg name="userCredentials" ref="mongoCredentials" /> </bean> <bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials"> <constructor-arg name="username" value="${mongo.username}" /> <constructor-arg name="password" value="${mongo.password}" /> </bean> <bean id="mongoDao" class="com.jiufang.dao.common.base.MongoDao"> <property name="mongoTemplate" ref="mongoTemplate" /> </bean> </beans>
    <!-- mongodb -->

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.6.1.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>2.12.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Tomcat9 配置在Windows7 64位 上安装步骤

下一篇:面试----java基础集合---------------------comparable和compara