随手记
问题背景
当项目确定技术中为springboot3(3.3.1),jdk17,vue3后,风风火火地开搞,当搞的差不多了,要连mongo时,才发现mongo服务版本为3.2.12,版本太低,以至于springboot3无法连接。
maven依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
报错信息:
org.springframework.data.mongodb.UncategorizedMongoDbException: Server at ip:port reports wire version 4, but this version of the driver requires at least 6 (MongoDB 3.6).
研究过程
springboot3-mongo的集成包。可以分解成以下依赖:
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>bson</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-core</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>4.3.1</version> </dependency>
尝试单独更换不同版本的驱动,不同版本的核心依赖包,结果都不行,与springboot3-mongo的集成包版本不匹配。最终放弃使用springboot集成的包
解决方案
使用单独的java-mongo依赖,引入spring-data依赖,让mongo对象可以给到spring去管理(IOC),一个可以的包集合如下:
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>4.7.2</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>bson</artifactId> <version>4.7.2</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-core</artifactId> <version>4.7.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>4.0.12</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>3.0.12</version> </dependency>
因为不和springboot去做绑定,所以需要自己处理连接mongo的账号密码,例子:MongoConfig.java
package xxxx.config; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; /** * @Description: 自定义一个mongoTemplate,交给springboot管理 * 文档:https://www.mongodb.com/zh-cn/docs/drivers/java/sync/v4.7/quick-start/ * https://docs.spring.io/spring-data/data-mongodb/reference/mongodb/template-collection-management.html * @Author hxh * @date 2024/8/2 14:30 */ @Configuration public class MongoConfig { @Value("${spring.data.mongodb.uri}") private String mongoUri; @Value("${spring.data.mongodb.database}") private String mongoDatabase; @Bean MongoClient mongoClient() { return MongoClients.create(mongoUri); } @Bean MongoOperations mongoTemplate(MongoClient mongoClient) { return new MongoTemplate(mongoClient, mongoDatabase); } }
mongo的实体类对象,可以像springboot集成那样,使用@Document,@Indexed注解,例子:
@Data @Document(collection = "test_collection") public class TestMongo{ @Indexed private String testId; @Indexed private String testIndex; private String content; }
用法:因为交给spring管理的是MongoOperations 类型,不是MongoTemplate,所以注入时要注意一下对象类型
@Autowired MongoOperations mongoTemplate; find方法: Query query = new Query(); query.addCriteria(Criteria.where("字段名称").is(字段值).and("字段名称").is(字段值)); mongoTemplate.find(query, 对象.class)
增删查改和集成springboot的用法一样,不过多赘述。
标签:mongo,spring,低版本,springframework,springboot3,mongodb,org,data From: https://www.cnblogs.com/caihonghai/p/18342664