亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot與MongoDB版本對照一覽

 更新時(shí)間:2024年06月03日 14:35:46   作者:一恍過去  
這篇文章主要介紹了SpringBoot與MongoDB版本對照一覽,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot與MongoDB版本對照參考

版本參考

參考地址:https://docs.spring.io/spring-data/mongodb/docs/3.2.4/reference/html/#compatibility.matrix

版本查詢

SpringBoot整合MongoDB時(shí),需要引入spring-boot-starter-data-mongodb依賴,為了保證兼容性,需要pring-boot-starter-data-mongodb版本與MongoDB對應(yīng)。

查詢流程如下截圖:

比如:

SpringBoot版本為2.3.2;

Spring-boot-starter-data-mongodb版本為:3.0.2

所以對應(yīng)的MongoDBServerVersion版本為:4.4.x

MongoDB整合Spring的版本選擇問題

如果項(xiàng)目使用的是springboot的那就非常輕松,直接一步到位!如果是springmvc的請繼續(xù)看!

最近在項(xiàng)目中整合MongoDB,項(xiàng)目用的是Spring,各種報(bào)錯(cuò),找不到方法啊,xml一直報(bào)紅啊,配置好,運(yùn)行的時(shí)候又是各種錯(cuò)誤,各種調(diào)試??偹阏鰜砹?,整的過程就不說了,這邊直接整理一下,什么版本要對應(yīng)什么版本。方便有需要的朋友能少走一個(gè)坑!

MongoDB的版本沒有關(guān)系,我都是直接用的最新版,主要問題是,mongoDB的java驅(qū)動(dòng)包版本以及跟spring整合的jar包版本。

首先來說下MongDB的java驅(qū)動(dòng)包,他有1.X的,2.X的,3.X的,這邊1.X的不討論,太久遠(yuǎn)了。我們討論2.X的,和3.X的,這邊貼下

MongoDB Java Driver 的版本

網(wǎng)頁太長。。湊活著看,大概意思就是說分為2.X和3.X版本。

為了方便大家,這個(gè)是Maven倉庫網(wǎng)址:https://mvnrepository.com/ 速度還挺快的,方便搜索。

接下來一一介紹:

MongoDB Java Drriver 2.X版本

對應(yīng)的Spirng框架版本是4.X的,使用的spring-data-mongodb是1.X的版本。千萬不要選2.x版本。會(huì)出問題。

這是maven配置:

        <!--MongoBD-Spring整合-->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>2.14.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.10.7.RELEASE</version>
        </dependency>

接下來是xml文件的頭文件:

這邊就直接貼上我自己的xml配置吧!對應(yīng)該版本號的:

<?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:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="
          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">
 
    <mongo:mongo replica-set="${mongo.hostport}">
 
        <mongo:options connections-per-host="${mongo.connectionsPerHost}"
                       threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                       connect-timeout="${mongo.connectTimeout}"
                       max-wait-time="${mongo.maxWaitTime}"
                       auto-connect-retry="true"
                       socket-keep-alive="true"
                       socket-timeout="${mongo.socketTimeout}"
                       slave-ok="true"
                       write-number="1"
                       write-timeout="0"
                       write-fsync="true"
        />
    </mongo:mongo>
    <mongo:db-factory username="root" password="root" dbname="admin" mongo-ref="mongo"/>
 
    <bean id="MongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    </bean>
</beans>

我是引用資源文件的,mongodb.properties,這是我的資源文件:

mongo.hostport=127.0.0.1:27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#連接超時(shí)時(shí)間
mongo.connectTimeout=1000
#等待時(shí)間
mongo.maxWaitTime=1500
#Socket超時(shí)時(shí)間
mongo.socketTimeout=1500
#admin登錄信息
mongo.username=root
mongo.password=root
#DataBaseName
#認(rèn)證數(shù)據(jù)庫名
mongo.authenticationDbname=admin
#要鏈接的數(shù)據(jù)庫名
mongo.databaseName=test

ok,都配置好以后,啟動(dòng)spring容器就能獲取MongTemplate對象,就能進(jìn)行操作了!這邊就不演示了,接著說下一個(gè)版本。

MongoDB Java Driver 3.X版本

跟spring整合的jar包版本是2.0.1.RELEASE,這個(gè)是我的maven:

<!--MongoBD-Spring整合-->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

注意?。?/p>

如果你用的是這個(gè)組的話,你的spring框架必須要用5.X的版本。不然會(huì)報(bào) java.lang.NoSuchMethodError: org.springframework.util.Assert.isTrue 的這個(gè)錯(cuò)誤。 說這個(gè)方法找不到!如果你的spring框架是5.X版本那就沒這個(gè)問題。

所有也要根據(jù)spring的框架版本來選擇。

這個(gè)是配置mongdb的xml文件,跟上面那個(gè)不同,我直接貼出來了:

<?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:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
                http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
                http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 
    <mongo:mongo-client id="mongoClient" replica-set="${mongo.hostport}"
                        credentials="${mongo.username}:${mongo.password}@${mongo.authenticationDbname}">
        <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-timeout="${mongo.socketTimeout}"
        />
    </mongo:mongo-client>
 
    <mongo:db-factory dbname="${mongo.databaseName}" mongo-ref="mongoClient"/>
 
    <mongo:template db-factory-ref="mongoDbFactory"/>
 
</beans>

心得

1.一定要根據(jù)自己spring的版本來選擇。

2.可以看得出來高版本的配置更簡潔,安全,方便!

3.建議選擇版本系列里面較新的版本,例如選擇了2.X的mongo-java驅(qū)動(dòng),建議選擇用的人多一點(diǎn)的版本,或2.X最新版。

題外話:配置完以后,我們是通過mongoTemplate對象來進(jìn)行增刪改查的操作。

只需自動(dòng)注入:

@Autowired
private MongoOperations mongoTemplate;

這里說下為什么用MongoOperations申明,這是來自官方的解釋:

引用MongoTemplate實(shí)例操作的首選方法是通過它的接口MongoOperations。 

貼個(gè)網(wǎng)址,Spring官方的整合MongoDB的教程,可以直接用谷歌翻譯看:

https://docs.spring.io/spring-data/mongodb/docs/2.0.0.RELEASE/reference/html/#mongo.mongo-3

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論