springboot与mongodb版本对照参考
版本参考
参考地址:https://docs.spring.io/spring-data/mongodb/docs/3.2.4/reference/html/#compatibility.matrix
版本查询
springboot
整合mongodb
时,需要引入spring-boot-starter-data-mongodb
依赖,为了保证兼容性,需要pring-boot-starter-data-mongodb
版本与mongodb
对应。
查询流程如下截图:
比如:
springboot版本为2.3.2;
spring-boot-starter-data-mongodb版本为:3.0.2
所以对应的mongodbserverversion版本为:4.4.x
mongodb整合spring的版本选择问题
如果项目使用的是springboot的那就非常轻松,直接一步到位!如果是springmvc的请继续看!
最近在项目中整合mongodb,项目用的是spring,各种报错,找不到方法啊,xml一直报红啊,配置好,运行的时候又是各种错误,各种调试。总算整出来了,整的过程就不说了,这边直接整理一下,什么版本要对应什么版本。方便有需要的朋友能少走一个坑!
mongodb的版本没有关系,我都是直接用的最新版,主要问题是,mongodb的java驱动包版本以及跟spring整合的jar包版本。
首先来说下mongdb的java驱动包,他有1.x的,2.x的,3.x的,这边1.x的不讨论,太久远了。我们讨论2.x的,和3.x的,这边贴下
mongodb java driver 的版本
网页太长。。凑活着看,大概意思就是说分为2.x和3.x版本。
为了方便大家,这个是maven仓库网址:https://mvnrepository.com/ 速度还挺快的,方便搜索。
接下来一一介绍:
mongodb java drriver 2.x版本
对应的spirng框架版本是4.x的,使用的spring-data-mongodb是1.x的版本。千万不要选2.x版本。会出问题。
这是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配置吧!对应该版本号的:
<?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 #连接超时时间 mongo.connecttimeout=1000 #等待时间 mongo.maxwaittime=1500 #socket超时时间 mongo.sockettimeout=1500 #admin登录信息 mongo.username=root mongo.password=root #databasename #认证数据库名 mongo.authenticationdbname=admin #要链接的数据库名 mongo.databasename=test
ok,都配置好以后,启动spring容器就能获取mongtemplate对象,就能进行操作了!这边就不演示了,接着说下一个版本。
mongodb java driver 3.x版本
跟spring整合的jar包版本是2.0.1.release,这个是我的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>
注意!!
如果你用的是这个组的话,你的spring框架必须要用5.x的版本。不然会报 java.lang.nosuchmethoderror: org.springframework.util.assert.istrue 的这个错误。 说这个方法找不到!如果你的spring框架是5.x版本那就没这个问题。
所有也要根据spring的框架版本来选择。
这个是配置mongdb的xml文件,跟上面那个不同,我直接贴出来了:
<?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.一定要根据自己spring的版本来选择。
2.可以看得出来高版本的配置更简洁,安全,方便!
3.建议选择版本系列里面较新的版本,例如选择了2.x的mongo-java驱动,建议选择用的人多一点的版本,或2.x最新版。
题外话:配置完以后,我们是通过mongotemplate对象来进行增删改查的操作。
只需自动注入:
@autowired private mongooperations mongotemplate;
这里说下为什么用mongooperations申明,这是来自官方的解释:
引用mongotemplate实例操作的首选方法是通过它的接口mongooperations。
贴个网址,spring官方的整合mongodb的教程,可以直接用谷歌翻译看:
https://docs.spring.io/spring-data/mongodb/docs/2.0.0.release/reference/html/#mongo.mongo-3
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论